Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Trying to set up custom schedules for WP Cron, knowing that they use an interval is it even possible to set up a cron job for every first of the month? As well as every fifteenth. This is what I have so far:

private function cron_schedules( $schedules ) {

    $midnight = strtotime( "midnight", current_time( 'timestamp' ) );
    $first = strtotime( 'first day of this month', $midnight );
    $fifteenth = $first + (7 * 24 * 60 * 60) * 2;

    $schedules['1st'] = array(
        'interval'  => $first,
        'display'   => __('1st of every month'),
    );

    $schedules['15th'] = array(
        'interval'  => $fifteenth,
        'display'   => __('15th of every month'),
    );

    $schedules['weekly'] = array(
        'interval'  => ( 7 * 24 * 60 * 60 ),
        'display'   => __('Weekly'),
    );

    $schedules['biweekly'] = array(
        'interval'  => ( 7 * 24 * 60 * 60 ) * 2,
        'display'   => __('Biweekly'),
    );

    return $schedules;

}
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

wp_cron operates on intervals and there is no interval that will hit exactly the first day and the 15th of every month.

You could run your wp-cron job every day and check the date, similar to this but with a simple callback like:

cron_callback_wpse_113675() {
  $date = date('d');
  if ('01' == $date || '15' == $date) {
    // run your function
  }
}

Or, use wp_schedule_single_event with a more complicated callback. Something like this:

function cron_callback_v2_wpse_113675() {
  $date = date('d');
  if (!wp_next_scheduled(cron_callback_v2_wpse_113675)) {
    $date = ($date < 15) ? '15' : '01';
  }
  if ('01' == $date || '15' == $date) {
    // run your function
    switch ($date) {
      case '01' :
        wp_schedule_single_event( strtotime('+14 days',strtotime('first day of')), 'cron_callback_v2_wpse_113675' );
      break;
      case '15' :
        wp_schedule_single_event( strtotime('first day of'), 'cron_callback_v2_wpse_113675' );
      break;
    }
  }
}

Barely Completely untested. Possibly buggy. Caveat emptor. No refunds.

share|improve this answer
    
Thanks for the answer! I found this as well wordpress.stackexchange.com/questions/41541/… wondering if I can set up my own custom scheduler. What are your thoughts? –  ftntravis Sep 11 '13 at 17:11
    
I have seen that 'transient' solution before but I don't see the advantage over using wp_schedule_single_event as above, assuming that untested code actually works. Maybe I am missing something though. –  s_ha_dum Sep 11 '13 at 17:22
    
Testing your first example right now, I'll let you know how it goes. Just wanted to see your thoughts on the other post. Thanks. –  ftntravis Sep 11 '13 at 17:25
    
Tested out and seems to working great so far. Thanks for the help! –  ftntravis Sep 13 '13 at 18:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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