Support » Fixing WordPress » Custom post type permalinks 404, and flushing the rewrite rules

  • I’m creating a theme with a custom post type, and taxonomy. Permalinks for this, obviously, doesn’t work till the rewrite rules are not flushed, and the rewrite rules – apparently – are not flushing themselves automatically after a new post type or taxonomy has been registered (or removed), so I have to do it myself.

    Working permalinks is something that should work right out of the box, so I can not expect of people using this theme to fix this issue for themselves by visit the permalinks.php page. That is just silly.

    Solution is adding a $wp_rewrite->flush_rules(); line after all custom post types/taxonomies has been registered. However, that happens on every init, which I don’t think is the right approach (flushing rewrites on each page load).

    Is there any pretty solution to this problem?

    The best thing possible would be an action hook firing on theme activation, but only after the init hook (after all has been registered). Unfortunately, I was not lucky enough to find anything like that.

Viewing 1 replies (of 1 total)
  • Jeremy Boggs

    (@jeremyboggs)

    The most elegant solution I’ve found is to set an option on your plugin’s activation, then check for that option in the init. Here’s a gist of what I mean. In a nutshell:

    1. Set an option called ‘my_plugin_flush_rules’ to ‘true’ on activation
    2. On init, when you create your custom post type, check that ‘my_plugin_flush_rules’ option, and flush your rules if it is true, then delete the option so it is only flushed once.

    The code I link to has more detailed comments. You could easily check this option on a variety of other actions available to plugins.

Viewing 1 replies (of 1 total)
  • The topic ‘Custom post type permalinks 404, and flushing the rewrite rules’ is closed to new replies.