admin管理员组

文章数量:1134241

I'm looking add-in a bit more speficity to the WP Cron intervals. To add a "weekly" interval, I've done the following:

function re_not_add_weekly( $schedules ) {
    $schedules['weekly'] = array(
        'interval' => 604800, //that's how many seconds in a week, for the unix timestamp
        'display' => __('weekly')
    );
    return $schedules;
}
add_filter('cron_schedules', 're_not_add_weekly');

Which works great, but - the extra sauce here is getting that cron to run on a specific day:

if( !wp_next_scheduled( 're_not_mail' ) ) {
    wp_schedule_event( time(), 'weekly', 're_not_mail' );
}

Anyone have any thoughts on the best way to accomplish this using WP Cron (assuming this isn't per a specific site that we'll have control over their cPanel/CRON area). Thanks!

Update

Going at this full-force and found an article that may have clarified things a bit more, but doesn't exactly answer my question. The basic gist of that article states that the WP Cron isn't as flexible (past the "hourly, daily, weekly" params), so extending it to something like weekly on a certain day seems a bit farfetched.

The issue (calling it an issue out of confusion/frustration) I have with that is -> sure, I could disable WP CRON and have WP CRON run once a week using the server CRON, BUT, that also means that the items that are normally run, like plugin/theme updates, post deletions/publishes based on CRON are put on a backlog for an entire week (if I wanted CRON to run once a week every Monday for example).

I'd have to assume others have come across this, so anymore insight on this would be a huge help. Thanks!

I'm looking add-in a bit more speficity to the WP Cron intervals. To add a "weekly" interval, I've done the following:

function re_not_add_weekly( $schedules ) {
    $schedules['weekly'] = array(
        'interval' => 604800, //that's how many seconds in a week, for the unix timestamp
        'display' => __('weekly')
    );
    return $schedules;
}
add_filter('cron_schedules', 're_not_add_weekly');

Which works great, but - the extra sauce here is getting that cron to run on a specific day:

if( !wp_next_scheduled( 're_not_mail' ) ) {
    wp_schedule_event( time(), 'weekly', 're_not_mail' );
}

Anyone have any thoughts on the best way to accomplish this using WP Cron (assuming this isn't per a specific site that we'll have control over their cPanel/CRON area). Thanks!

Update

Going at this full-force and found an article that may have clarified things a bit more, but doesn't exactly answer my question. The basic gist of that article states that the WP Cron isn't as flexible (past the "hourly, daily, weekly" params), so extending it to something like weekly on a certain day seems a bit farfetched.

The issue (calling it an issue out of confusion/frustration) I have with that is -> sure, I could disable WP CRON and have WP CRON run once a week using the server CRON, BUT, that also means that the items that are normally run, like plugin/theme updates, post deletions/publishes based on CRON are put on a backlog for an entire week (if I wanted CRON to run once a week every Monday for example).

I'd have to assume others have come across this, so anymore insight on this would be a huge help. Thanks!

Share Improve this question edited Sep 12, 2012 at 14:15 Zach asked Sep 10, 2012 at 18:00 ZachZach 1,9435 gold badges31 silver badges54 bronze badges 2
  • What exactly is a certain day? Does it start in Greenwich UNIX time at 00:00:00? Or somewhere else? – kaiser Commented Sep 10, 2012 at 18:17
  • Hi @kaiser - let's go with the assumption that this should be run weekly, each Monday at 7:00am EDT. Does that clarify things a bit better? Thanks! – Zach Commented Sep 10, 2012 at 18:36
Add a comment  | 

5 Answers 5

Reset to default 2

WP-Cron is not intended to be that precise, and should not be used if you have to have things scheduled at specific times. WP-Cron is a "best effort" scheduling mechanism, and it cannot guarantee run timing like a real cron system can.

If you need precision of this nature, the only real answer is to not use WP-Cron for it. It's not designed for that, and it cannot do it. Any hacky code you attempt to add to it to make it capable of this won't fix that underlying problem.

Use a real cron system.

If you have important WP cron jobs on a schedule you should of course have a system cron job running that regularly calls WP cron to make things are always fired when you need them.

If you need a specific time/date for your WP cron runs you can always schedule a single event and have the called method schedule the next event.

Simple example:

function some_awesome_hook() {
    // Do stuff here

    // Run next Monday
    $next_run = strtotime('next monday');

    // Clear hook, just in case
    wp_clear_scheduled_hook('some_awesome_hook');

    // Add our event
    wp_schedule_single_event($next_run, 'some_awesome_hook');
}

// Run next Monday
$first_run = strtotime('next monday');

// Add our event
wp_schedule_single_event($first_run, 'some_awesome_hook');

OOP style:

class My_Sweet_Plugin {

    public $cron_hook;

    public function __construct() {
        // Store our cron hook name
        $this->cron_hook = 'my_awesome_cron_hook';

        // Install cron!
        $this->setup_cron();

        // Add action that points to class method
        add_action($this->cron_hook, array($this, 'my_awesome_function'));
    }

    public function setup_cron() {
        // Clear existing hooks
        wp_clear_scheduled_hook($this->cron_hook);

        // Next run time
        $next_run = strtotime('next monday');

        // Add single event
        wp_schedule_single_event($first_run, $this->cron_hook);
    }

    public function my_awesome_function() {
        // Do stuff!

        // Setup our next run
        $this->setup_cron();
    }
}

Even though this is quite an old post, accurate scheduling problem remains, so I thought it worthwhile to add a solution that I use to work around this. Hopefully it helps get a bit more resolution on scheduling (i.e. more specificity).

While the WordPress Cron is not designed to execute on a particular day or time every week, there is a way to work around this and get reasonably good results.

My example will be to schedule the Cron to run on a particular day of the week (Sunday) at a particular time (02:00), but you can modify this to make it run on any particular day in the month, day in the year, whatever.

First set the custom recurrence period in the usual way (if you need a custom schedule):

function my_add_custom_time( $schedules ) {
  $schedules['weekly'] = array(
    'interval' => 604800,
    'display' => __('Every Week')
  );
  return $schedules;
}
add_filter('cron_schedules', 'my_add_custom_time');

Next, assign the schedule to your Cron hook.

The logic is as follows... When setting up the schedule, check if the event has already been scheduled, if not, schedule to the next possible Sunday at 2:00. If it is already scheduled, check that the day is a Sunday. If the day was wrong then re-schedule the Cron to run on the next Sunday at 2:00.

function my_custom_cron_settings() {
  $next_stamp = strtotime('next sunday + 2 hour');
  $timestamp = wp_next_scheduled('my_cron_hook');
  if ( !$timestamp ) {
    wp_schedule_event( $next_stamp, 'weekly', 'my_cron_hook' );
  } else {
    $day = date("D", $timestamp);
    if ( $day != 'Sun' ){
        wp_clear_scheduled_hook('my_cron_hook');
        wp_schedule_event( $next_stamp, 'weekly', 'my_cron_hook' );
    }
  }
}
add_action( 'wp', 'my_custom_cron_settings' );

Finally, set up the Cron hook action to do whatever actions you want to perform.

function my_cron_do_whatever(){
  //do whatever you want the Cron to do here
}
add_action( 'my_cron_hook', 'my_cron_do_whatever' );

This code can easily be modified to handle other scenarios, and while not a perfect solution, it may help if you want a bit more control over your Cron Job.

According to Codex you can use wp_get_schedules to add in an option like Weekly (which you've already done).

WP-Cron was not intended to have all the Cron functions so why not just login to the server and create a cron job to call your PHP file only on Monday and add something like this to your crontab

* 07 * * Mon root cmd

You can use this approach

//Your cron function which you want to execute any specific day, e.g. Monday
function daily_cron_callback() {
    $timestamp = time();    
    
    if(date('D', $timestamp) !== 'Mon') { //do nothing, if not Monday
        return;
    }
    
    //Process your logic here
}

本文标签: Run WP Cron Weekly (but on a certain day)