add_feed and flush_rewrite_rules

There seems to be quite a controversial opinion on the WordPress flush_rewrite_rules() function.

According to the Codex, it is highly discouraged to use this function on the init action hook.

Read More

However:

In a plugin I am working on, I am using add_feed() to, well, add a feed. The Codex says that this should be run on init and requires a one-time use of flush_rewrite_rules().

See my dilemma ? I need add_feed() on init, which requires flush_rewrite_rules().

Hence, flush_rewrite_rules() on init, which is breaking all custom post type rewrite rules and permalinks, resulting in 404 pages.

So my question is: how should this be done ?

This is what I currently have on init, that is related to the issue:

add_action( 'init', 'my_foobar' );
function my_foobar() {
    // getting some settings
    add_feed( $url, $callback );
    flush_rewrite_rules();
}

Any help would be greatly appreciated. thanks 🙂

Related posts

1 comment

  1. You have to check the existing rewrite rules before you run a flush. If your feed name is test, they are stored under the keys 'feed/(feed|rdf|rss|rss2|atom|test)/?$' and '(feed|rdf|rss|rss2|atom|test)/?$'.

    So this should do the trick:

    add_action( 'init', function()
    {    
        $name       = 'test';
        $registered = FALSE;
    
        add_feed( $name, 'test_feed' );
    
        $rules = get_option( 'rewrite_rules' );
        $feeds = array_keys( $rules, 'index.php?&feed=$matches[1]' );
    
        foreach ( $feeds as $feed )
        {
            if ( FALSE !== strpos( $feed, $name ) )
                $registered = TRUE;
        }
    
        // Feed not yet registered, so lets flush the rules once.
        if ( ! $registered )
            flush_rewrite_rules( FALSE );
    });
    

Comments are closed.