• I’m working on a WordPress plugin which is utilising the WP_Rewrite class to add a custom rewrite rule. The rewrite rule is added during the “init” action and is activated upon plugin activation.

    This works like a charm, but for some reason the rule disappears after a couple of hours. Visiting the “Permalinks” page in WP Admin fixes the problem for a couple of hours.

    The URL should look like: site.com/contact/netherlands. “Contact” is a page with ID 49248, this page contains a shortcode which activates the plugin. The plugin should generate the appropriate HTML.

    I hook into the “init” action this way:

    add_action( 'init', array( $this, 'init' ), 10 );
    add_action( 'init', array( $this, 'refreshRewriteRules' ), 20 );

    Which calls this method:

    public function refreshRewriteRules( )
    {
        if ( get_option( 'resellers_flush_rewrite_rules_flag' ) )  {
            flush_rewrite_rules();
            delete_option( 'resellers_flush_rewrite_rules_flag' );
        }
    }
    
    public function init()
    {
        add_rewrite_rule(
            'contact/([^/]*)$',
            'index.php?page_id=49248&c=$matches[1]',
            'top'
        );
    }

    Is there any way to detect why and when the rules are flushed?

The topic ‘Rewrite rules disappearing’ is closed to new replies.