Tag Archives: programming

In WordPress, when you register a custom post type, having a slug is mandatory out-of-the-box. There are a few of reasons for this, but the most important one is performance. If you have conflicting rewrite rules, you can make them all work by hooking into the process and running some queries to see which rule is the intended target of the current path. These queries aren’t free, and in the world of performance, every bit counts. With that in mind, removing slugs is ill-advised. Sometimes there are restrictions beyond your control, and you have to go against your better judgement to do so anyway, so for those cases, here’s how you can remove the slug from custom post type permalinks.

Step 1: Remove the default rewrite rules

When you register a post type, if the ‘rewrite’ argument is not set to false, WordPress will add rewrite rules for you. Normally this is a good thing, but in this case, it’s not going to do so according to our master plan, so we need to disable this. When registering your post type, set the ‘rewrite’ argument to false.

Step 2: Manually add the rewrite rules

When register_post_type is called, WordPress does two main things to add rewrite rules for that post type: it adds a rewrite tag, and adds a permastruct. Since we’re asking WordPress not to do this, we’ll have to do so manually. The rewrite tag will depend on if your post type is hierarchical or not, so your code will look something like this (this should happen on init):

Step 3: Rearrange the generated rewrite rules

Now your rewrite rules are added, but they’re added relatively high in the rewrite array. This is going to cause unnecessary conflicts with other rewrite rules, so we need to move them down the chain a bit. Unfortunately, WordPress doesn’t provide a clean way to do this, so we have to do something a little “hacky” to get the rules in the order we want. Specifically, we need to pull our rules out and inject them into the rules array at a later point in the chain. Since this is an associative array in a specific order, this is easier said than done. We’ll first hook into the {$post_type}_rewrite_rules filter and pull out the rules for this post type, storing them for later use:

Step 4: Resolve conflicting rewrite rules on-the-fly

Currently, our rewrite rules are working, but pages are not. That’s because WordPress thinks that every page is an entry in our custom post type due to the conflicting rules. To resolve this, we need to hook into the request action and potentially manipulate the generated query vars.

Final notes

That’s the gist of the work we needed to do. It doesn’t cover everything, but should illustrate the important points of what needs to be done. Below I’ve included a class that combines all this and also resolves a few edge cases not addressed or discussed above.

The only other point worth noting is that you can’t do this to more than one post type, at least not without some significant hacking. You see, WordPress stores rewrite rules as an associative array of regex => redirect. Because the regular expression is the array key, you cannot have two identical expressions (having two would be pointless anyway). Are you out of luck? No, but I’ll save that for a future post (dun dun dun… cliffhanger!).

Post Type Icons is a radically simple plugin that gives you 361 wonderful icons courtesy of Font Awesome. The icons are font-based, so they’re vector in nature and look amazing no matter your screen’s DPI. Now your custom post types can have custom icons that look just like the core icons in every state (previously, it was difficult to get the hover/active state working quite right). This will be in the WordPress Plugin Repository as soon as it’s approved!

I recently launched a new plugin on the WordPress plugin repository: SuperCPT. SuperCPT is a WordPress Plugin to help developers build insanely easy and attractive custom post types, custom post meta boxes and fields, and custom taxonomies. Install it from the WordPress.org Plugin Repository.

I’m currently reading The Pragmatic Programmer: From Journeyman to Master by Andrew Hunt and Dave Thomas on my Kindle (Bezos, I love you, this thing is pretty awesome). Right, the book — It’s a quality book so far, and I find it hilarious to think that it was written some 12 years ago. 12 years in software development is a significant portion of the field’s entire life! It’s a testament to how important and unchanging the fundamentals are (when you ge them right). Of course, I’m only a short way through the book at this point, so maybe it ends with a 1999-worthy gem like, “640K ought to be enough for anybody.” Continue reading →