Menu

WP No Category Base Not Working – Permalink Custom Structure with Static Front

Disclaimer up front: this post will only help you if your permalinks settings look like mine. There may be other reasons why your WP No Category Base plugin is broken. This post deals with a specific issue of conflicting permalink settings and WP No Category Base code.

Cut to the chase. Here’s the code you need.

if (version_compare($wp_version, '3.4', '<')) { // For pre-3.4 support $wp_rewrite -> extra_permastructs['category'][0] = $wp_rewrite->front . '%category%';
} else {
	$wp_rewrite -> extra_permastructs['category']['struct'] = $wp_rewrite->front . '%category%';
}

Modify the plugin’s main PHP file, called no-category-base.php and replace the existing lines 49-54 (careful with the line numbers, YMMV). Swap out those lines with the code above and you’re done.

“But I thought you said never to modify a plugin!” I can hear you whining. I know I did. And it’s still good advice. In this case I believe it’s a necessary evil. I will have to remember to replace my modified code if/when WP No Category Base gets updated. I’ve made backups of the original and my modified PHP files for future copypasta. I still feel dirty for doing it though.

Ok, I see what you did there. Now explain it me.

No problem, anonymous internetter. The magic is in this one little bit:

$wp_rewrite->front // Ok.... but, like, what is that?

The Codex says “WP_Rewrite is WordPress’ class for managing the rewrite rules that allow you to use Pretty Permalinks feature.” If you’re using WP No Category Base, you must be using some version of pretty permalinks. Therefore, you’re unknowingly making use of WP’s WP_Rewrite class. We need to grab a piece of that class and put it to work for us.

Again from the Codex, the front property is “anything up to the start of the first tag in your $permalink_structure.” Or, in clearer English, that refers to anything you’ve put before “%category%” in the Custom Structure field in your Permalinks settings. Like so:

WP No Category Base - Custom Structure.

The original plugin code looks like this, around line 49:

if (version_compare($wp_version, '3.4', '<')) { // For pre-3.4 support $wp_rewrite -> extra_permastructs['category'][0] = '%category%';
} else {
	$wp_rewrite -> extra_permastructs['category']['struct'] = '%category%';
}

After we insert the “front” property, we get this:

if (version_compare($wp_version, '3.4', '<')) { // For pre-3.4 support $wp_rewrite -> extra_permastructs['category'][0] = $wp_rewrite->front . '%category%';
} else {
	$wp_rewrite -> extra_permastructs['category']['struct'] = $wp_rewrite->front . '%category%';
}

I’ll admit that I haven’t read through wp-includes/rewrite.php to learn exactly how $wp_rewrite works, but I understand enough to see that the original code strips away everything but %category% in the $wp_rewrite base. The modified code makes sure that the front property is included in that category base.

Credit where credit is due

I didn’t come up with this solution. That credit belongs to WordPress user maximinime and his/her answer in the WP support forum. The thread is closed, so I can’t post there to say thanks, but I wish I could. Maximinime’s suggestion has bailed me out on several occasions. I’m reposing it here for better visibility. I hope it helps you too!