How to correct schedule my event weekly with wp_schedule_event()

According to the docs for this function, you can’t set up a weekly event by default.

wp_schedule_event(int $timestamp, string $recurrence, string $hook, $args = array() );

The $recurrence value needs to be one of:

  • “hourly”
  • “twicedaily”
  • “daily”

You’ll need to test today’s date inside your callback function, my_event. Something like:

function my_event() {
  # date('l') returns the formatted full day of the week 
  if ( date ('l') !== 'Friday' ) {
    return;
  }
  # do my_event things!
}

Edit:

If you want to add a weekly cron schedule, you can do it by adding a filter to the cron_schedules filter hook. You can see an example of that here, it would work out to something like this:

function 246184_weekly_cron_schedule( $schedules ) {
    $schedules[ 'weekly' ] = array( 
        'interval' => 60 * 60 * 24 * 7, # 604,800, seconds in a week
        'display' => __( 'Weekly' ) );
    return $schedules;
}
add_filter( 'cron_schedules', '246184_weekly_cron_schedule' );

Leave a Comment