0

I wrote the folowing timer in my function.php file. It should be called every certain time (3 minutes) but it doesn't work at all.

function myprefix_custom_cron_schedule( $schedules ) {

 $schedules['every_three_minutes'] = array(
      'interval' => 180, 
      'display'  => __( 'Every 3 minutes' ),
  );
  return $schedules;
}
add_filter( 'cron_schedules', 'myprefix_custom_cron_schedule' );

if ( ! wp_next_scheduled( 'myprefix_cron_hook' ) ) {
  wp_schedule_event( time(), 'every_three_minutes', 'myprefix_cron_hook' );
}

add_action( 'myprefix_cron_hook', 'do_anything' );

and here is my function:

function do_anything($user_id) {
  update_user_meta( $user_id, 'alg_wc_ev_is_activated', '0' );
  wp_redirect(  wc_get_page_permalink( 'myaccount' ) );
  exit();
}

I don't know, why it doesn't work. Am I missing something in code?

2
  • 1
    I'm not sure you understand how WP Scheduling works. It runs in the background, and has no connection to whoever is logged in at the time. It's meant to do stuff "behind the scenes". It doesn't know what your $user_id is, and it can't redirect, because it isn't connected to a user session.
    – vancoder
    Feb 23, 2021 at 18:26
  • Thans for your feedback. is there any way that I can update a value in DB after a certain time? Feb 24, 2021 at 7:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.