admin管理员组文章数量:1393556
I am writing a script to add a named Cron job that updates a single user, that runs every 5 minutes or so.
My problem is that the job runs for every user over and over again every second or so. Here is the code that I have placed inside my functions.php file.
This is my first foray into the WP Cron functionality with WordPress and would like to know if I set up the jobs correctly.
function so_custom_cron_schedule( $schedules ) {
$schedules['every_5_minutes'] = array(
'interval' => 300,
'display' => __( 'Every 5 minutes' ),
);
return $schedules;
}
add_filter( 'cron_schedules', 'so_custom_cron_schedule' );
function update_social_user($user_id){
$user = get_userdata($user_id);
if(!$user){
return;
}
var_error_log('running for '.$user_id);
}
function assign_cron(){
$users = get_users([ 'role__in' => [ 'administrator', 'seller'] ]);
$args = array(false);
foreach($users as $user){
$hook_name = 'update_fb_'.$user->ID;
add_action($hook_name,'update_social_user');
if(!wp_next_scheduled($hook_name,$args)){
wp_schedule_event(time(),'every_5_minutes',$hook_name,array($user->ID));
}else{
var_error_log('Already set');
}
}
}
assign_cron();
I am writing a script to add a named Cron job that updates a single user, that runs every 5 minutes or so.
My problem is that the job runs for every user over and over again every second or so. Here is the code that I have placed inside my functions.php file.
This is my first foray into the WP Cron functionality with WordPress and would like to know if I set up the jobs correctly.
function so_custom_cron_schedule( $schedules ) {
$schedules['every_5_minutes'] = array(
'interval' => 300,
'display' => __( 'Every 5 minutes' ),
);
return $schedules;
}
add_filter( 'cron_schedules', 'so_custom_cron_schedule' );
function update_social_user($user_id){
$user = get_userdata($user_id);
if(!$user){
return;
}
var_error_log('running for '.$user_id);
}
function assign_cron(){
$users = get_users([ 'role__in' => [ 'administrator', 'seller'] ]);
$args = array(false);
foreach($users as $user){
$hook_name = 'update_fb_'.$user->ID;
add_action($hook_name,'update_social_user');
if(!wp_next_scheduled($hook_name,$args)){
wp_schedule_event(time(),'every_5_minutes',$hook_name,array($user->ID));
}else{
var_error_log('Already set');
}
}
}
assign_cron();
Share
Improve this question
asked Feb 9, 2020 at 16:15
frank Collinsfrank Collins
534 bronze badges
1 Answer
Reset to default 0I usually define a IF state and use wp_clear_scheduled_hook
function. See example:
if (get_option('atenasupervisor_scheduler') <> 'Stop') {
echo 'Active ('. get_option('atenasupervisor_scheduler').')</strong></div>';
} else {
wp_clear_scheduled_hook( 'atenasupervisor_my_email' );
echo 'Not active ('. get_option('atenasupervisor_scheduler').')</strong></div>';
}
``
本文标签: WP Cron jobs loops infinitely
版权声明:本文标题:WP Cron jobs loops infinitely 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744764484a2623953.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论