2

if I'm using the REST API to create endpoints in my plugin, should I be flushing rewrite rules on init or in rest_api_init?

I realize that it should be done on plugin activation/deactivation. However, once either of those evens are triggered, don't you still have to respect the traditional hook process in order to ensure the rewrite rules are flushed AFTER they've been modified? A/K/A - After the rest routes have been added.

Example:

function activate() {

    // do something when this plugin is activated...

    add_action( 'init', function () {

        flush_rewrite_rules( true );
    }, 9999 );

    return true;

}

# Plugin Activation
register_activation_hook( __FILE__, function () {

    require_once( PATH . '/backend/inc/activation.php' );
    Backend\activate();

} );

init

add_action( 'init', function () {

    flush_rewrite_rules( true );
}, 9999 );

rest_api_init

add_action( 'rest_api_init', function () {

    flush_rewrite_rules( true );
}, 9999 );
0

Flushing rewrite rule should not be done on routine basis, as stated per codex:

Don't do it on any hook that will triggered on a routine basis.

You should either do it via the plugin activation hooks, or the theme switch hooks:

add_action( 'after_switch_theme', 'wpse315001_flush_rewrite_rules' );

register_deactivation_hook( __FILE__, 'wpse315001_flush_rewrite_rules' );
register_activation_hook( __FILE__, 'wpse315001_flush_rewrite_rules' );
function wpse315001_flush_rewrite_rules() {
    flush_rewrite_rules();
}

Either of the above, and it should be done AFTER you have registered your custom post types.

Also, you should flush the rewrite rules after your plugin is deactivated. This makes sure no invalid rule is left.

However, like every other problem, this has a nasty workaround too. If your rewrite rules contain a semi-unique keyword, you can get the rewrite rules from the options, run a search on it and flush the rules only if necessary:

add_action( 'init', 'wpse315001_flush_rewrite_rules', 99999 );
function wpse315001_flush_rewrite_rules(){

    // Get the rewrite rules
    $rules = get_option('rewrite_rules');

    // Check if your semi-unique rule exists in this string
    if ( ! strpos( $rules, 'your-rewrite-rules' ) ) {
        // Flush'em All!
        flush_rewrite_rules();
    }
}

Maybe not the best solution, but it'll do.

  • I realize that it should be done on plugin activation/deactivation. However, once either of those evens are triggered, don't you still have to respect the traditional hook process in order to ensure the rewrite rules are flushed AFTER they've been modified? A/K/A - After the rest routes have been added. – Michael Ecklund Sep 24 '18 at 16:32
  • Digging through the rewrite rule class, I can see that once you call it, the rules are immediately flushed. Also, the codex strictly mentions that this must be done after registering the post types, probably for the very same reason. Some genius developer might be registering the post types on a different hook, on a theme, or even 999999 priority. – Jack Johansson Sep 24 '18 at 16:39
  • @MichaelEcklund I've also updated my answer for you. – Jack Johansson Sep 24 '18 at 16:46
  • call the function that registers the content types from within the wpse315001_flush_rewrite_rules function, then flush rules. – Milo Sep 24 '18 at 16:47
  • @Milo I assume that's not the case, since that would easily solve the problem. I guess the CPT is being added by some other plugins. – Jack Johansson Sep 24 '18 at 16:48

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.