Site icon Hip-Hop Website Design and Development

Flush Rewrite Rules on init or rest_api_init?

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

} );

init

add_action( 'init', function () {

    flush_rewrite_rules( true );
}, 9999 );

rest_api_init

add_action( 'rest_api_init', function () {

    flush_rewrite_rules( true );
}, 9999 );