每10分钟更新一次用户元数据



我创建了一些自定义用户元,其中有一个值。我希望该值或用户在其中的任何值每10分钟更新一次,并用"锁定"一词替换。

要执行每十分钟运行一次的cron作业,下面是一个代码示例:

您可以使用https://generatewp.com生成类似的函数

// Schedule Cron Job Event
add_action( 'init', 'my_custom_cron_job' );
function my_custom_cron_job() {
if ( ! wp_next_scheduled( 'myprefix_update_user_phone_cron' ) ) {
wp_schedule_event( time(), 'ten_minutes', 'myprefix_update_user_phone_cron' );
}
}
// Scheduled Action Hook
add_action( 'myprefix_update_user_phone_cron', 'myprefix_update_user_phone' );
function myprefix_update_user_phone( ) {
// Do what you want
error_log('TEST MY CRON');
}
// Custom Cron Recurrences
function custom_cron_job_recurrence( $schedules ) {
$schedules['ten_minutes'] = array(
'display' => __( 'Every 10 minutes', 'textdomain' ),
'interval' => 600,
);
return $schedules;
}
add_filter( 'cron_schedules', 'custom_cron_job_recurrence' );
// Schedule Cron Job Event
add_action( 'init', 'my_custom_cron_job' );
function my_custom_cron_job() {
if ( ! wp_next_scheduled( 'myprefix_update_user_phone_cron' ) ) {
wp_schedule_event( time(), 'ten_minutes', 'myprefix_update_user_phone_cron'     );
}
}
// Scheduled Action Hook
add_action( 'myprefix_update_user_phone_cron', 'myprefix_update_user_phone' );
function myprefix_update_user_phone( ) {
// Do what you want
$website = 'http://wordpress.org';
update_user_meta($user_id, 'user_url', $website);
}
// Custom Cron Recurrences
function custom_cron_job_recurrence( $schedules ) {
$schedules['ten_minutes'] = array(
'display' => __( 'Every 10 minutes', 'textdomain' ),
'interval' => 300,
);
return $schedules;
}
add_filter( 'cron_schedules', 'custom_cron_job_recurrence' );
// Scheduled Action Hook
add_action( 'myprefix_update_user_phone_cron', 'myprefix_update_user_phone' );
function myprefix_update_user_phone( ) {
// Do what you want
$users = get_users(); 
foreach($users as $user){ 
$website = 'wordpress.org'; 
myprefix_update_user_phone($user->ID, '$user_url', $website);
}
}

最新更新