Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 自定义WP计划事件_Wordpress - Fatal编程技术网

Wordpress 自定义WP计划事件

Wordpress 自定义WP计划事件,wordpress,Wordpress,专家们 目前在我的一个wordpress插件中有一个cron,它按照下面的时间表运行。 wp_计划_事件(time(),'hourly','w4pr_cron'); 我想将此更改为在特定时间范围内每5分钟运行一次 例如,您知道的当前代码将在一天中每小时运行一次。(一天24次)。 我希望在早上5:00到早上6:30(美国东部标准时间)之间每5分钟运行一次(一天18次) 感谢您的帮助 谢谢 Thomas您首先需要添加自定义cron计划,使其每5分钟运行一次 // Add cron interval

专家们

目前在我的一个wordpress插件中有一个cron,它按照下面的时间表运行。 wp_计划_事件(time(),'hourly','w4pr_cron'); 我想将此更改为在特定时间范围内每5分钟运行一次

例如,您知道的当前代码将在一天中每小时运行一次。(一天24次)。 我希望在早上5:00到早上6:30(美国东部标准时间)之间每5分钟运行一次(一天18次)

感谢您的帮助

谢谢
Thomas

您首先需要添加自定义cron计划,使其每5分钟运行一次

 // Add cron interval of 60 seconds
function addCronMinutes($array) {
    $array['minute'] = array(
            'interval' => 60,
            'display' => 'Once a Minute',
    );
    return $array;
}
add_filter('cron_schedules','addCronMinutes');
但是,为了只允许它在特定的时间范围内运行,而不造成额外的开销,您最好不要安排每个特定的事件


另一个选项是在触发器中放入逻辑,以便仅在指定的时间范围内运行您的函数,但这会在高流量站点上造成额外的开销,在这些站点上,cron将每5分钟触发一次。

首先创建一个自定义间隔,您可以找到有关此的详细信息

请确保您未计划插件注册的事件:

wp_unschedule_event( next_scheduled_event( 'w4pr_cron' ), 'w4pr_cron' );
下一步,将此重复间隔的事件安排在上午5点开始:

if( time() > strtotime( 'today 5:00' ) )
    wp_schedule_event( strtotime( 'tomorrow 5:00' ), '5min', 'w4pr_cron' );
else
    wp_schedule_event( strtotime( 'today 5:00' ), '5min', 'w4pr_cron' );
w4pr_cron不是一个函数,而是一个带有附加函数的钩子。因此,如果时间戳不在给定的时间间隔内,那么您要做的是确保在调用这个钩子时不会发生任何事情。因此,在functions.php或在每次加载页面时都会看到它执行的地方,放置:

add_action( 'init', function() {
    $start = strtotime( 'today 5:00' );
    $end = strtotime( 'today 6:30' );
    if( !(time() > $start && time() < $end) ) remove_all_actions( 'w4pr_cron' );
}
添加操作('init',函数(){ $start=strottime('today 5:00'); $end=strottime('today 6:30'); 如果(!(time()>$start&&time()<$end))删除所有操作(“w4pr\u cron”); }
add_action( 'init', function() {
    $start = strtotime( 'today 5:00' );
    $end = strtotime( 'today 6:30' );
    if( !(time() > $start && time() < $end) ) remove_all_actions( 'w4pr_cron' );
}