url rewriting - Flush Rewrite Rules on init or rest_api_init?

admin2025-01-07  5

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

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 );
Share Improve this question edited Mar 4, 2022 at 12:21 bueltge 17.1k7 gold badges61 silver badges97 bronze badges asked Sep 24, 2018 at 13:57 Michael EcklundMichael Ecklund 6,81213 gold badges68 silver badges110 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736259460a590.html

最新回复(0)