4

A common problem with rewrite rules added in a theme is that there is no obvious hook on which you can flush the rewrite rules. The problem is getting harder when you update a theme with new code that includes new rewrite rules.

It seems like a good solution to the problem might be to detect if the rewrite rule exists and if it doesn't to flush the rules. So how can I check if a rule already exists?

  • Great question! This should be part of the core API and be mentioned in the flush_rewrite_rules() docs. People have been dancing around the issue for years, such a bummer. – jerclarke Apr 25 '17 at 20:27
4

If I understand correctly then you can hook into the rewrite api/process and flush or manipulate the rules that way? Read: Plugin Hooks on this page

http://codex.wordpress.org/Function_Reference/WP_Rewrite

Maybe something like:

// flush_rules() if our rules are not yet included
function my_flush_rules()
{
    $rules = get_option( 'rewrite_rules' );
    if ( ! isset( $rules['(project)/(\d*)$'] ) ) { 
        global $wp_rewrite; $wp_rewrite->flush_rules();
    }
}
add_action( 'wp_loaded','my_flush_rules' );
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.