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, 2017 at 20:27

1 Answer 1


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


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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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