Numeric Slugs Revisited

|

I like to use numbers in my URLs instead of text because it makes more sense to me. Comic pages go up numerically, and I catalog my illustrations by the custom date number in the lower right corner of the site. However, version 4.3 of WordPress introduced a bit of a snag in that setup. No longer is it possible to have a number as the first element of a clean permalink structure. According to the code itself in wp-includes/post.php:

/*
* Potential date clashes are as follows:
*
* – Any integer in the first permastruct position could be a year.
* – An integer between 1 and 12 that follows ‘year’ conflicts with ‘monthnum’.
* – An integer between 1 and 31 that follows ‘monthnum’ conflicts with ‘day’.
*/

This means that in WordPress 4.3+, the numbers I use to make permalinks will always result in them getting a “-2” appended. Well, that just won’t do.

I wrote about this dilemma back in December (two news posts ago) and figured out the first half of a solution by hacking the core code. Of course, hacking the core code is a really bad idea, so I’ve been tinkering with a plugin or functions.php solution to resolve the issue and I think I’ve come up with a solution.

add_filter('wp_unique_post_slug', 'eljo_unique_post_slug', 1, 6);

function eljo_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
	
	$post = get_post($post_ID);
	if($post_type === 'post'
		&& (
			!$post
				||
			$post->post_name !== $original_slug
		)
		&&
		preg_match('/^[0-9]+$/', $original_slug)
		&&
		$slug_num = intval($original_slug)) {
			
			// don't allow two or four digit integer slugs
			if((preg_match('/^\d{2}$/', $slug_num))
				||
			(preg_match('/^\d{4}$/', $slug_num))) {
			
				return $slug;
			
			}
						
		}
	
	$slug = $original_slug;
	return $slug;

}


Because years in just about every existing calendar system (except perhaps the French Republican) use four digits, and months generally only have two digits, I decided to have WordPress not allow two or four digit slugs. But it will allow any other integer length. That means I can continue to use my three and five digit post slugs and I (hopefully) won’t cause a date conflict for WordPress. This code works on my local dev site with WP 4.4.2.

If you prefer using numbers for your permalink slugs, maybe this is the code you’re looking for.

Scam Alert

An illustration friend of mine received an email from a guy named James Floyd who wanted to hire her to build a website for him. She doesn’t build websites but she knows that I do, so she gave me a heads up about it on Facebook. I fired off an email of my own saying that I could build this site, and he was super happy about it. He emailed me the details in the following email:


Hello-

Thanks for your response. I have small scale business which i want to turn into large scale business now it located in MI and the company is based on importing and exporting of Agriculture products such as Kola Nut, Gacillia Nut and Cocoa so i need a best of the best layout design for it. I hope you can handle that for me. Check out this site: http://www.agroamerica.com, I will be happy if you can give me something better than this if its possible. The site would only be informational, so i need you to give me an estimate based on the site i gave you to check out. The estimate should include hosting and i want the same page as the site i gave you to check out. I have a private project consultant, he has the text content and the logos for the site.

Note:

1. I want the same number of pages with the example site i gave you to check excluding videos and blogs.
2. I want only English language
3. I don't have a domain yet but i want the domain name as JayProfarm.org
4. you will be updating the site for me.
5. i will be proving the images, logos and content for the site.
6. i want the site up and running before ending of next month.
7. My budget is $3000 to $7000

Kindly get back to me with:

I. An Estimate

II. Cell-Phone Number

Regards,

James.

So far, so good. I wasn’t feeling any sketchiness. Yet. I told him my plan for building the site, and gave him an estimate and (sadly) my phone number. He got back the next day with this email:


Hi-

Thanks for your response. I'm okay with the estimate and i want to proceed so i will be depositing $2000.00 using credit card so work can commence ASAP. I understand the text content and logos for this site would be needed so as for the job to commence regarding the text content and the logos, I will need a LITTLE FAVOR from you. The reason i need this favor from you is because the consultant does not have the facility to charge credit cards and i also am presently in the hospital for surgery. I will be glad if you can help me out with this favor. I'll await your respond. Thanks and God bless you.

Regards,
James.

KLAXON! WHOOP WHOOP! RED ALERT! SHIELDS UP!

See those two words all in caps: “LITTLE FAVOR”? Yeah, that’s code for “this is totally a scam”. I did a web search for James Floyd and discovered that a James Floyd Breeding was the assistant to Secretary of Agriculture, Grain and Feed Division in the 1960s. But no James Floyd with an import/export business in kola nut, gacillia nut and cocoa. Then I did a web search for those terms and found two websites, both of web designers, saying that this was most definitely a scam.

Check out these blog posts for more info about this scam:
How I nearly got scammed!
Scam Targets Web Designers

And now I have a blog post of my own about it. I’ll help get the word out for fellow web designers to keep their eyes open and be ever vigilant for those red flags. Nobody wants to be scammed, especially hard working freelancers.

Edit: Turns out lots of folks have been approached with this scam. The lesson I learned today is always do a web search on a project to make sure it’s on the level before committing to it.

Numeric Slugs

|

It’s getting to be the end of 2015, and so I figured I’d update my website theme for the new year. I use MAMP to make a local install of WordPress on my Mac and then build the theme using the most recent version. As of this writing, the most recent version of WordPress is 4.4. However, I found out that in version 4.3, numeric post slugs began appending a “-2” to the slug to prevent possible conflicts with date-based permalinks.

Sadly, this change completely screws up my permalink structure for my portfolio and webcomic sites. Sure, it would be easy enough to add an additional element to the URL such as “http://ellejohara.com/threads/page/001/” where I add “page”, but I don’t really want to do that.

I picked through the source code and found the new lines that do the check for date conflicts. After puzzling through it for an hour or so, I figured out how to work around the check.

In wp-includes/post.php, the lines of code added to check the permalink structure for numbers begins on line 3616. I’ll comment on what the code does line-by-line.


// Prevent new post slugs that could result in URLs that conflict with date archives.
$post = get_post( $post_ID );
// Get the post ID

$conflicts_with_date_archive = false;
// Make a conflict catcher variable we can use for later

if ( 'post' === $post_type && ( ! $post || $post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) && $slug_num = intval( $slug ) ) {
// if the type of post is 'post' AND there is either no post id or the post name is not the same as the post slug AND the slug contains numbers AND those numbers can be successfully converted into an integer, then do the following:

$permastructs = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) );
// Get the permalink structure and split its parts into an array

$postname_index = array_search( '%postname%', $permastructs );
// Look for the array value '%postname%' and get its matching key

/*
* Potential date clashes are as follows:
*
* - Any integer in the first permastruct position could be a year.
* - An integer between 1 and 12 that follows 'year' conflicts with 'monthnum'.
* - An integer between 1 and 31 that follows 'monthnum' conflicts with 'day'.
*/
// This is a big one, so I'll break it down a bit

if ( 0 === $postname_index
// if '%postname%' is the only element in the permalink structure

|| // OR

( $postname_index && '%year%' === $permastructs[ $postname_index - 1 ] && 13 > $slug_num )
// there is more than one element in the permalink structure AND '%year%' comes before '%postname%' AND the slug integer is less than 13 (1 - 12)

|| // OR

( $postname_index && '%monthnum%' === $permastructs[ $postname_index - 1 ] && 32 > $slug_num )
// there is more than one element in the permalink structure AND '%monthnum%' comes before '%postname%' AND the slug integer is less than 32 (1 - 31)

) { // THEN

$conflicts_with_date_archive = true;
// set the conflict catcher variable to TRUE

}
}

This $conflicts_with_date_archive variable is then used a few lines later in the if statement that checks bad slugs. If it’s a bad slug, add a 2 to the slug to prevent a possible date-based permalink conflict.

The code clearly states that any integer slug could be interpreted as a date, so it calls it bad and adds the 2. But I figure dates really only need either a two-digit or a four-digit number, one represents days or months, and the other represents the year. So if only two or four-digit numbers are needed for date-based permalinks, then why shut out all numbers?

After line 3619, I added another if statement with a few preg_match rules to look for the number of digits in the slug integer. If it finds a single digit, two digits, or four digits, then it marks the slug as a date conflict and adds the two. If the integer slug is three digits, or five or more digits, there is no conflict and you get the numeric slug you want. Here’s what I did:


if((preg_match('/^\d{1}$/', $slug_num)) || (preg_match('/^\d{2}$/', $slug_num)) || (preg_match('/^\d{4}$/', $slug_num))) {

Don’t forget to add another closing curly bracket after line 3636 or you’ll break PHP.

WordPress seemed to work just fine for me for years and years with three-digit and five-digit slugs, and I’m hoping it will continue to work now that I’ve added this additional if statement. If you plan on using this fix to get numeric slugs working again, I don’t recommend hacking the WordPress source code itself to do it. It would be better to hook the function with a custom one in the functions.php file in your theme, or to make a plugin. Since I’ve never bothered learning how to make WordPress plugins, I don’t have one here to download. Sorry. But hopefully this will spark the imagination of others to do so.

I just wanted my numeric slugs back.

New Old Theme

I’ve finally gotten around to building a new custom theme for my art site. Next up is to make some new art to put on the new art site.

Testing!

Cake, and grief counseling, will be available at the conclusion of the test.