Which one do you recommend using within a plugin and why?

add_action( 'wp', 'trigger_me' );
function trigger_me() {

    if ( !wp_next_scheduled( 'my_plugin_cron' ) ) {
        wp_schedule_event(time(), 'hourly', 'my_plugin_cron');
    }

}

OR

add_action( 'init', 'trigger_me' );
function trigger_me() {

    if ( !wp_next_scheduled( 'my_plugin_cron' ) ) {
        wp_schedule_event(time(), 'hourly', 'my_plugin_cron');
    }

}

What are the advantages/disadvantages of “wp” over “init” when registering/triggering cron function within a plugin?

1
1

Neither.

register_activation_hook( __FILE__, 'trigger_me' );

function trigger_me() {

    if ( !wp_next_scheduled( 'my_plugin_cron' ) ) {
        wp_schedule_event(time(), 'hourly', 'my_plugin_cron');
    }

}

Why parse code on every request when you don’t need to?

Leave a Reply

Your email address will not be published. Required fields are marked *