3

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

  • How do you check if this is run only once? – Krzysiek Dróżdż Dec 19 '18 at 18:41
  • only one user meta row is created with 92 as ID, another row ought to be created every 5 seconds – johnson Dec 19 '18 at 19:02
  • You use update_user_meta, so it won't create any other rows. It should update existing row... – Krzysiek Dróżdż Dec 19 '18 at 19:11
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.

  • Although the cron works even if it's hooked into plugin activation but your comment on the reason why only one meta will be stored in the DB helped. Thanks – johnson Dec 19 '18 at 20:28
1

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');
    }
?>
  • hi, yeah i refreshed the page and it's not working – johnson Dec 19 '18 at 18:26
  • I updated my answer, as a test try running that code – RiddleMeThis Dec 19 '18 at 18:40
  • did that, still the same. Another row won't be created in the users meta database, only one row is created with 92 as ID – johnson Dec 19 '18 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.