I am having a bit of a strange issue with rewrite rules not flushing properly.
I have tried using flush_rewrite_rules();
and flush_rewrite_rules(true);
.
I have also tried globalizing $wp_rewrite
using $wp_rewrite->flush_rules();
and $wp_rewrite->flush_rules(true);
Neither of which appear to be flushing the rewrite rules correctly. Those calls are indeed flushing the rewrite rules when called. How do I know this? Using the solution for debugging rewrite rule flushing.
Currently, I have rewrite rules flushed on plugin activation and plugin deactivation. No issues there.
I have an plugin administration settings page for users to configure the plugin. Some of the settings adjust the permalink structure, so the rewrite rules are required to be flushed on plugin administration settings page “Save Settings”. (Uses the standard update_option();
) for saving settings.
I would like to note that depending on the settings specified, custom post types are created to match user specified settings. So the rewrite rules must be flushed immediately after the settings are saved. This is where things aren’t functioning appropriately.
The above link solution for debugging rewrite rules provided by @toscho
is displaying that it’s flushing tons of rewrite rules. However, when visiting the custom post type singular item, or even custom post type archive for that matter, each return as 404 errors.
The custom post type is registered correctly and appropriately. I know for certain that is not the issue.
Immediately following with the plugin administration page settings save. The custom post types are created, permalink structure is adjusted, and all rewrite rules are attempted to be flushed.
The custom post types are then loaded always, and loaded on init
like normal.
For some reason, the rewrite rules aren’t flushing properly, because like I said before, visiting singular or archive sections of the custom post type return 404 errors.
Now the weird part, if all I do is simply visit the administration permalinks settings page, and then go back to the front end to view either singular or archive sections of the custom post type, they magically work as expected.
What does that administration permalinks settings page do that I’m not doing that allows the rewrite rules to flush appropriately and mine do not?
I mean, as a temporary solution, I am redirecting the user to the administration permalinks settings page after saving the plugin administration settings page, but this is not an ideal solution. I’d prefer the rewrite rules just flush properly within my plugin’s code.
Is there a certain point in WordPress where flushing the rewrite rules just doesn’t flush ALL the rules anymore?
admin_menu
– Plugin settings page is added to WordPress administration.
add_options_page()
– Plugin settings page is added under Settings menu.
Settings page is rendered in the callback for add_options_page()
. This is also where $_POST
is processed for updating plugin settings and flushing rewrite rules.
Since this is already a long question, I would be willing to provide code blocks (if it helps) in an offsite link that to assist producing a valid answer.
The best place to flush rewrite rules is on plugin activation/deactivation.
function myplugin_activate() {
// register taxonomies/post types here
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'myplugin_activate' );
function myplugin_deactivate() {
flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'myplugin_deactivate' );
See the codex article
Apologies in advance, I didn’t make it all the way through your question, so this is a bit of a cookie cutter response.