设置WordPress Cron事件,假设您想在下午6点启动Cron,并在每15分钟后继续运行Cron,直到晚上9点(持续3小时)。然后所有cron停止。
Note: 为简化实现,代码中的所有时间条目都被视为GMT/UTC。
为此,您需要安排两个cron事件,其中一个将在下午6点开始,并从下午6点开始每隔15分钟运行一次。另一个是将在晚上9点运行的一次性cron事件。这一个将阻止另一个cron。
插件代码如下所示:
<?php
/*
Plugin Name: WPSE Custom Cron
Plugin URI: https://wordpress.stackexchange.com/a/309973/110572
Description: Custom Cron Plugin
Version: 1.0.0
Author: Fayaz Ahmed
Author URI: https://www.fayazmiraz.com/
*/
add_action( \'wpse_custom_cron_event\', \'wpse_custom_cron\' );
add_action( \'wpse_custom_cron_event_stop\', \'wpse_custom_cron_stop\' );
function wpse_custom_cron() {
// Your Custom Cron CODE HERE
}
function wpse_custom_cron_start() {
// Calculate the start time: e.g. whenever the next 6:00PM (UTC) is.
$cron_start_time = strtotime( "today 6:00pm" );
// If 6PM has already passed today, set it to 6PM next day
if( $cron_start_time < time() ) {
$cron_start_time = $cron_start_time + 24 * HOUR_IN_SECONDS;
}
if ( ! wp_next_scheduled( \'wpse_custom_cron_event\' ) ) {
wp_schedule_event( $cron_start_time, \'fifteen_minutes\', \'wpse_custom_cron_event\' );
}
if ( ! wp_next_scheduled( \'wpse_custom_cron_event_stop\' ) ) {
// this 1 time cron will stop the original cron \'wpse_custom_cron_event\' after 3 hours of starting
$cron_stop_time = $cron_start_time + 3 * HOUR_IN_SECONDS;
wp_schedule_single_event( $cron_stop_time, \'wpse_custom_cron_event_stop\' );
}
}
function wpse_custom_cron_stop() {
// removing all possible custom cron events named \'wpse_custom_cron_event\'
while( false !== wp_unschedule_event( wp_next_scheduled( \'wpse_custom_cron_event\' ), \'wpse_custom_cron_event\' ) ) {}
}
// Add a 15 minutes custom cron schedule
add_filter( \'cron_schedules\', \'wpse_custom_cron_schedule\' );
function wpse_custom_cron_schedule( $schedules ) {
$schedules[\'fifteen_minutes\'] = array(
\'interval\' => 15 * 60,
\'display\' => esc_html__( \'Every Fifteen Minutes\' ),
);
return $schedules;
}
// schedule the cron event on plugin activation
register_activation_hook( __FILE__, \'wpse_custom_cron_plugin_activation\' );
function wpse_custom_cron_plugin_activation() {
wpse_custom_cron_start();
}
// remove the cron event on plugin deactivation
register_deactivation_hook( __FILE__, \'wpse_custom_cron_plugin_deactivation\' );
function wpse_custom_cron_plugin_deactivation() {
wpse_custom_cron_stop();
// in case the stop event didn\'t run yet
while( false !== wp_unschedule_event( wp_next_scheduled( \'wpse_custom_cron_event_stop\' ), \'wpse_custom_cron_event_stop\' ) ) {}
}
此示例插件将在激活插件时启动cron,但如果需要,您也可以使用
wpse_custom_cron_start()
作用
此外,如果您想每天下午6点运行同一个cron(而不仅仅是激活后的一次),那么只需更改wp_schedule_single_event
请来wpse_custom_cron_start()
功能到:
wp_schedule_event( $cron_stop_time, \'daily\', \'wpse_custom_cron_event_stop\' );
Note: Event/Cron created with arguments
如果使用参数创建事件/cron,
you must also stop the event with the exact same argument.例如,如果您创建了这样的事件(在wpse_custom_cron_start()
上述代码的功能):
....
wp_schedule_event( $cron_start_time, \'fifteen_minutes\', \'wpse_custom_cron_event\', $args1 );
....
wp_schedule_single_event( $cron_stop_time, \'wpse_custom_cron_event_stop\', $args2 );
然后,在停止事件时,还必须在中使用完全相同的参数
wp_next_scheduled()
函数调用。因此,停止代码将变为:
....
while( false !== wp_unschedule_event( wp_next_scheduled( \'wpse_custom_cron_event\', $args1 ), \'wpse_custom_cron_event\' ) ) {}
....
while( false !== wp_unschedule_event( wp_next_scheduled( \'wpse_custom_cron_event_stop\', $args2 ), \'wpse_custom_cron_event_stop\' ) ) {}
再一次,记住,它必须是
exact same argument, 即使不同的数据类型也无法工作。例如,在以下代码中,
$args1
和
$args2
是
NOT 同样,但是
$args1
和
$args3
相同:
$args1 = array( \'key\' => 1 );
$args2 = array( \'key\' => \'1\' );
$args3 = array( \'key\' => 1 );
因为
\'1\'
是字符串和
1
是一个数字。
这很重要,因为,sometimes people save the arguments in database as key value pairs, &;当他们稍后再次使用它时,来自数据库的值对数字参数不起作用,因为从数据库检索时数字会转换为字符串。因此,在将参数传递给wp_next_scheduled()
作用
有关更多信息,请查看文档: