问题描述
我想创建一个每3分钟运行一次的函数。
I want to create a function which the function run in every 3 minute.
我使用插件wp-crontrol创建一个cron作业事件。
I use plugin wp-crontrol to create a cron job event.
我在Google和Stackoverflow中进行了搜索,而function.php中的代码为
I searched in Google and Stackoverflow, and the code in function.php is
// Add a new interval of 180 seconds
// See http://codex.wordpress.org/Plugin_API/Filter_Reference/cron_schedules
add_filter( 'cron_schedules', 'isa_add_every_three_minutes' );
function isa_add_every_three_minutes( $schedules ) {
$schedules['every_three_minutes'] = array(
'interval' => 180,
'display' => __( 'Every 3 Minutes', 'textdomain' )
);
return $schedules;
}
// Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'isa_add_every_three_minutes' ) ) {
wp_schedule_event( time(), 'every_three_minutes', 'isa_add_every_three_minutes' );
}
// Hook into that action that'll fire every three minutes
add_action( 'isa_add_every_three_minutes', 'every_three_minutes_event_func' );
function every_three_minutes_event_func() {
// The code run update in every 3 minutes
$update= get_field('ls_home_number_candidates');
$updatePlusOne= $update++;
update_post_meta(56, 'ls_home_number_candidates', $updatePlusOne);
}
?>
然后在我的WP-Cron活动中出现了一个新活动,名为 isa_add_every_three_minutes
Then in my WP-Cron Events appears the new event called isa_add_every_three_minutes
但是该事件未运行。如果我按立即运行,它将显示一条通知,例如成功执行了cron事件isa_add_every_three_minutes。
But the event does not run. If I press "Run now", it's will appear a announce like "Successfully executed the cron event isa_add_every_three_minutes."
但是它不起作用,并且字段 Next Run 始终为现在,
But it's not work and the field Next Run is always "now"
请帮助。谢谢
推荐答案
代码中的函数 isa_add_every_three_minutes
只是为了获得您设置的自定义时间表,与cron名称无关。您的 add_action
应该具有cron名称以及要执行的cron函数,然后您需要对其进行调用,例如:
The function isa_add_every_three_minutes
in your code is just to get the custom schedules you set, it's not related to the cron name. Your add_action
should have the cron name, and the cron function you want to execute, and then you need to call it, so something like:
// Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'my_cron' ) ) {
wp_schedule_event( time(), 'every_three_minutes', 'my_cron' );
}
// Hook into that action that'll fire every three minutes
add_action( 'my_cron', 'every_three_minutes_event_func' );
表面上的其他东西看起来都很好。
Everything else looks fine on the surface.
这篇关于所述“下一次运行”是指,在wp crontrol插件中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!