[wp-hackers] About "Post Type Voodoo" (a plugin for WordPress 2.9+)

Mike Schinkel mikeschinkel at newclarity.net
Tue Dec 8 05:37:56 UTC 2009

Hi all,

So after all the debate on custom urls and custom post types I've been working on a plugin during all my free time for the past 3 days.  I started with the HTDIR plugin Dion wrote and took those lessons an have greatly embellished.


I've tried to incorporate the example below that Otto provided but after many hours of working could not get it to work for my use cases:

function plugin_add_custom_urls() {
add_rewrite_rule('products/(.*)[/]?$', 'index.php?product=$matches[1]', 'top');
add_action('init', 'plugin_add_custom_urls');

function plugin_add_template() {
$product = get_query_var('product');
if (!empty($product)) {
  locate_template(array("products/$product.php","products/index.php"), true);
add_action('template_redirect', 'plugin_add_template');

I originally started down the path of creating a generic URL rewriting component but have since decided to first focus on what I believe is probably 80% of the need for custom URLs and that's for custom post types.  So I decided to build a plugin that focuses on enabling custom post types that of course first handles custom URLs for custom post types.  

After much work I've learned a lot and even challenged some of my own original assumptions.  One of the things I learned was how incredibly hard it is to get URL rewriting working in WordPress.  Maybe that's why this comment can be found in core for the WP_Rewrite() function:

* The main WP_Rewrite function for building the rewrite rule list. The
* contents of the function is a mix of black magic and regular expressions,
* so best just ignore the contents and move to the parameters.

One of the biggest problems is making sure things are initialized before you run them.  I started with a hard-coded proof of concept and made it work, and as I refactored to make it more generic it kept braking because things I needed hadn't been loaded yet.  I also realized that what I really needed was to define post types in the admin and along with the post types I needed to define their permalinks too. I still strongly believe this should be in the core but since so many people on this list debate that I've just decided to write my own plugin for it which I call "Post Type Voodoo."  (Why "Voodoo?"  Because it was much harder than it should have been to implement, kinda like voodoo, and because people act like it such a big deal that it can't possibly be put into core that it must be voodoo.)

Anyway, I realized  the approach of calling an add_custom_url() in the theme's functions.php is really not workable or even desirable. What's really needed is a place in the admin where post types can be defined and also a (ideally shared) in the database where post types and related info can be persisted.  (If not stored in the database then the load order problem becomes more an issue and also plugins wanting to add post types will potentially trample on each other.  Don't say I didn't warn you...)

I've also uncovered some questions.  If we have custom post types and we have the ability to tag and categories custom post types (as we should) then what should these urls display for the posts of a custom type tagged "foo" and categorized as "bar", respectively (include or not include the custom post types?)


And if we have a custom post type named "product" with a product named "widget" will it's default URL not have date and time components (I assume "no"), i.e.:


And will this URL work for all products tagged "premium" and if so does it exclude us from having a product named "tag?"


And will we need to have a different edit page for each post type of can we use a consolidated edit page, i.e.


 And so on... (I'm sure I'll have more later.)

Anyway, I've run into things I still can't figure out so wanted to ask some questions which I'll do in follow up emails.

-Mike Schinkel
P.S. The "Post Type Voodoo" is currently a work in progress which I'll release once I get to "minimum viable product" stage.  If anyone is interested in reviewing as-is, please email me directly.

More information about the wp-hackers mailing list