admin管理员组文章数量:1130349
Here is my code
function cron_add_weekly( $schedules ) {
$schedules['seconds'] = array(
'interval' => 5,
'display' => __( '5 Seconds' )
);
return $schedules;
}
add_filter( 'cron_schedules', 'cron_add_weekly' );
register_activation_hook(__FILE__, 'my_activation');
function my_activation() {
if ( ! wp_next_scheduled( 'my_hook' ) ) {
wp_schedule_event( time(), 'seconds', 'my_hook' );
}
}
add_action( 'my_hook', 'my_exec' );
register_deactivation_hook(__FILE__, 'my_deactivation');
function my_deactivation() {
wp_clear_scheduled_hook('my_hook');
}
function my_exec() {
$value = 91;
update_user_meta($value + 1, 'from_cron', 'updated');
}
The user meta ought to be updated every 5 seconds after refreshing the page. But the cron runs only the first time
Here is my code
function cron_add_weekly( $schedules ) {
$schedules['seconds'] = array(
'interval' => 5,
'display' => __( '5 Seconds' )
);
return $schedules;
}
add_filter( 'cron_schedules', 'cron_add_weekly' );
register_activation_hook(__FILE__, 'my_activation');
function my_activation() {
if ( ! wp_next_scheduled( 'my_hook' ) ) {
wp_schedule_event( time(), 'seconds', 'my_hook' );
}
}
add_action( 'my_hook', 'my_exec' );
register_deactivation_hook(__FILE__, 'my_deactivation');
function my_deactivation() {
wp_clear_scheduled_hook('my_hook');
}
function my_exec() {
$value = 91;
update_user_meta($value + 1, 'from_cron', 'updated');
}
The user meta ought to be updated every 5 seconds after refreshing the page. But the cron runs only the first time
Share Improve this question edited Dec 19, 2018 at 22:06 fuxia♦ 107k39 gold badges255 silver badges461 bronze badges asked Dec 19, 2018 at 18:08 johnsonjohnson 333 bronze badges 3 |2 Answers
Reset to default 0OK, so I've tested your code and I'm pretty sure it can't run even once... And here's why...
If you'll take a look at wp_schedule_event you'll see this check at the top of the function:
if ( !isset( $schedules[$recurrence] ) )
return false;
It means that you can't schedule event with unknown recurrence.
So let's go back to your code... What it does is:
- adds your custom recurrence (using
cron_scheduleshook), - schedules an event with that recurrence during plugin activation.
Everything look good, right? Well, no - it does not.
Activation hook is fired during plugin activation. So that plugin is not working before the activation is run. So your custom recurrence is not registered. So... your hook is not scheduled. I've tested that and the result of wp_schedule_event in your activation hook is false, so the event is not scheduled.
So what to do?
Simple - don't schedule your event on activation hook, if you want to use custom recurrence.
So here's your safer code:
register_deactivation_hook(__FILE__, 'my_deactivation');
function my_deactivation() {
wp_clear_scheduled_hook('cron_every_5_seconds');
}
function add_every_5_seconds_cron_schedule( $schedules ) {
$schedules['every_5_seconds'] = array(
'interval' => 5,
'display' => __( 'Every 5 Seconds', 'textdomain' )
);
return $schedules;
}
add_filter( 'cron_schedules', 'add_every_5_seconds_cron_schedule' );
function schedule_my_cron_events() {
if ( ! wp_next_scheduled( 'cron_every_5_seconds') ) {
wp_schedule_event( time(), 'every_5_seconds', 'cron_every_5_seconds' );
}
}
add_action( 'init', 'schedule_my_cron_events' );
function cron_every_5_seconds_action() {
$value = 91;
update_user_meta( $value + 1, 'from_cron', 'updated' );
}
add_action( 'cron_every_5_seconds', 'cron_every_5_seconds_action' );
PS. (but it may be important) There is one more flaw with your code, but maybe it's just cause by some edits before posting here...
You won't be able to check if your cron runs only once or many times. Your event action updates user meta. But the new value that is set is always equal 91. And since you use update_user_meta, only one such meta will be stored in the DB.
Assuming your code above is correct (I didn't test it) it is important to note that wp-cron will only run when someone visits the site, so it will not run every 5 seconds in the background, like you might be thinking.
As a work around you can disable wp-cron and then implement a real cron job.
Here is a good article on this topic.
EDIT: Try adding it like this
function cron_every_5_seconds( $schedules ) {
$schedules['every_5_seconds'] = array(
'interval' => 5,
'display' => __( 'Every 5 Seconds', 'textdomain' )
);
return $schedules;
}
add_filter('cron_schedules', 'cron_every_5_seconds');
if (! wp_next_scheduled( 'cron_every_5_seconds')) {
wp_schedule_event(time(), 'every_5_seconds', 'cron_every_5_seconds');
}
add_action('cron_every_5_seconds', 'cron_every_5_seconds_action');
function cron_every_5_seconds_action() {
$value = 91;
update_user_meta($value + 1, 'from_cron', 'updated');
}
?>
Here is my code
function cron_add_weekly( $schedules ) {
$schedules['seconds'] = array(
'interval' => 5,
'display' => __( '5 Seconds' )
);
return $schedules;
}
add_filter( 'cron_schedules', 'cron_add_weekly' );
register_activation_hook(__FILE__, 'my_activation');
function my_activation() {
if ( ! wp_next_scheduled( 'my_hook' ) ) {
wp_schedule_event( time(), 'seconds', 'my_hook' );
}
}
add_action( 'my_hook', 'my_exec' );
register_deactivation_hook(__FILE__, 'my_deactivation');
function my_deactivation() {
wp_clear_scheduled_hook('my_hook');
}
function my_exec() {
$value = 91;
update_user_meta($value + 1, 'from_cron', 'updated');
}
The user meta ought to be updated every 5 seconds after refreshing the page. But the cron runs only the first time
Here is my code
function cron_add_weekly( $schedules ) {
$schedules['seconds'] = array(
'interval' => 5,
'display' => __( '5 Seconds' )
);
return $schedules;
}
add_filter( 'cron_schedules', 'cron_add_weekly' );
register_activation_hook(__FILE__, 'my_activation');
function my_activation() {
if ( ! wp_next_scheduled( 'my_hook' ) ) {
wp_schedule_event( time(), 'seconds', 'my_hook' );
}
}
add_action( 'my_hook', 'my_exec' );
register_deactivation_hook(__FILE__, 'my_deactivation');
function my_deactivation() {
wp_clear_scheduled_hook('my_hook');
}
function my_exec() {
$value = 91;
update_user_meta($value + 1, 'from_cron', 'updated');
}
The user meta ought to be updated every 5 seconds after refreshing the page. But the cron runs only the first time
Share Improve this question edited Dec 19, 2018 at 22:06 fuxia♦ 107k39 gold badges255 silver badges461 bronze badges asked Dec 19, 2018 at 18:08 johnsonjohnson 333 bronze badges 3- How do you check if this is run only once? – Krzysiek Dróżdż Commented Dec 19, 2018 at 18:41
- only one user meta row is created with 92 as ID, another row ought to be created every 5 seconds – johnson Commented Dec 19, 2018 at 19:02
-
You use
update_user_meta, so it won't create any other rows. It should update existing row... – Krzysiek Dróżdż Commented Dec 19, 2018 at 19:11
2 Answers
Reset to default 0OK, so I've tested your code and I'm pretty sure it can't run even once... And here's why...
If you'll take a look at wp_schedule_event you'll see this check at the top of the function:
if ( !isset( $schedules[$recurrence] ) )
return false;
It means that you can't schedule event with unknown recurrence.
So let's go back to your code... What it does is:
- adds your custom recurrence (using
cron_scheduleshook), - schedules an event with that recurrence during plugin activation.
Everything look good, right? Well, no - it does not.
Activation hook is fired during plugin activation. So that plugin is not working before the activation is run. So your custom recurrence is not registered. So... your hook is not scheduled. I've tested that and the result of wp_schedule_event in your activation hook is false, so the event is not scheduled.
So what to do?
Simple - don't schedule your event on activation hook, if you want to use custom recurrence.
So here's your safer code:
register_deactivation_hook(__FILE__, 'my_deactivation');
function my_deactivation() {
wp_clear_scheduled_hook('cron_every_5_seconds');
}
function add_every_5_seconds_cron_schedule( $schedules ) {
$schedules['every_5_seconds'] = array(
'interval' => 5,
'display' => __( 'Every 5 Seconds', 'textdomain' )
);
return $schedules;
}
add_filter( 'cron_schedules', 'add_every_5_seconds_cron_schedule' );
function schedule_my_cron_events() {
if ( ! wp_next_scheduled( 'cron_every_5_seconds') ) {
wp_schedule_event( time(), 'every_5_seconds', 'cron_every_5_seconds' );
}
}
add_action( 'init', 'schedule_my_cron_events' );
function cron_every_5_seconds_action() {
$value = 91;
update_user_meta( $value + 1, 'from_cron', 'updated' );
}
add_action( 'cron_every_5_seconds', 'cron_every_5_seconds_action' );
PS. (but it may be important) There is one more flaw with your code, but maybe it's just cause by some edits before posting here...
You won't be able to check if your cron runs only once or many times. Your event action updates user meta. But the new value that is set is always equal 91. And since you use update_user_meta, only one such meta will be stored in the DB.
Assuming your code above is correct (I didn't test it) it is important to note that wp-cron will only run when someone visits the site, so it will not run every 5 seconds in the background, like you might be thinking.
As a work around you can disable wp-cron and then implement a real cron job.
Here is a good article on this topic.
EDIT: Try adding it like this
function cron_every_5_seconds( $schedules ) {
$schedules['every_5_seconds'] = array(
'interval' => 5,
'display' => __( 'Every 5 Seconds', 'textdomain' )
);
return $schedules;
}
add_filter('cron_schedules', 'cron_every_5_seconds');
if (! wp_next_scheduled( 'cron_every_5_seconds')) {
wp_schedule_event(time(), 'every_5_seconds', 'cron_every_5_seconds');
}
add_action('cron_every_5_seconds', 'cron_every_5_seconds_action');
function cron_every_5_seconds_action() {
$value = 91;
update_user_meta($value + 1, 'from_cron', 'updated');
}
?>
本文标签: WP CRON runs only the first time
版权声明:本文标题:WP CRON runs only the first time 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749083158a2313363.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


update_user_meta, so it won't create any other rows. It should update existing row... – Krzysiek Dróżdż Commented Dec 19, 2018 at 19:11