$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>WP CRON runs only the first time|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

WP CRON runs only the first time

matteradmin9PV0评论

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
Add a comment  | 

2 Answers 2

Reset to default 0

OK, 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_schedules hook),
  • 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');
    }
?>

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far