apply_filters( 'cron_schedules', array[] $new_schedules )

Filters the non-default cron schedules.


Parameters

$new_schedules

(array[]) An array of non-default cron schedule arrays. Default empty.


Top ↑

More Information

The filter accepts an array of non-default cron schedules in arrays (an array of arrays). The outer array has a key that is the name of the schedule (for example, ‘weekly’). The value is an array with two keys, one is ‘interval’ and the other is ‘display’.

The ‘interval’ is a number in seconds of when the cron job shall run. So, for a hourly schedule, the ‘interval’ value would be 3600 or 60*60. For for a weekly schedule, the ‘interval’ value would be 60*60*24*7 or 604800.

The ‘display’ is the description of the non-default cron schedules. For the ‘weekly’ key, the ‘display’ may be __(‘Once Weekly’).

Why is this important?

When scheduling your own actions to run using the WordPress Cron service, you have to specify which interval WordPress should use. WordPress has its own, limited, default set of intervals, or “schedules”, including ‘hourly’, ‘twicedaily’, and ‘daily’. This filter allows you to add your own intervals to the default set.

For your plugin, you will be passed an array, you can easily add a weekly schedule by doing something like:

function my_add_weekly( $schedules ) {
	// add a 'weekly' schedule to the existing set
	$schedules['weekly'] = array(
		'interval' => 604800,
		'display' => __('Once Weekly')
	);
	return $schedules;
}
add_filter( 'cron_schedules', 'my_add_weekly' ); 

Adding multiple intervals works similarly:

function my_add_intervals($schedules) {
	// add a 'weekly' interval
	$schedules['weekly'] = array(
		'interval' => 604800,
		'display' => __('Once Weekly')
	);
	$schedules['monthly'] = array(
		'interval' => 2635200,
		'display' => __('Once a month')
	);
	return $schedules;
}
add_filter( 'cron_schedules', 'my_add_intervals'); 

Be sure to add your schedule to the passed array, as shown in the example. If you simply return only your own schedule array then you will potentially delete schedules created by other plugins.


Top ↑

Source

File: wp-includes/cron.php

View on Trac



Top ↑

Changelog

Changelog
Version Description
2.1.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by Dominik Schilling

    Add a custom cron schedule for every 5 minutes:

    /**
     * Adds a custom cron schedule for every 5 minutes.
     *
     * @param array $schedules An array of non-default cron schedules.
     * @return array Filtered array of non-default cron schedules.
     */
    function devhub_custom_cron_schedule( $schedules ) {
    	$schedules[ 'every-5-minutes' ] = array( 'interval' => 5 * MINUTE_IN_SECONDS, 'display' => __( 'Every 5 minutes', 'devhub' ) );
    	return $schedules;
    }
    add_filter( 'cron_schedules', 'devhub_custom_cron_schedule' );
    
  2. Skip to note 2 content
    Contributed by dhl17

    custom cron schedule for every 10 & 15 Seconds

    function custom_cron_job_recurrence( $schedules ) 
    {
    	if(!isset($schedules['10sec']))
    	{
    		$schedules['10sec'] = array(
    			'display' => __( 'Every 10 Seconds', 'twentyfifteen' ),
    			'interval' => 10,
    		);
    	}
    	
    	if(!isset($schedules['15sec']))
    	{
    		$schedules['15sec'] = array(
    		'display' => __( 'Every 15 Seconds', 'twentyfifteen' ),
    		'interval' => 15,
    		);
    	}
    	
    	return $schedules;
    }
    add_filter( ‘cron_schedules’, ‘custom_cron_job_recurrence’ );
    
  3. Skip to note 3 content
    Contributed by Ihtisham Zahoor

    Add a user-defined interval value:
    Using functinal approach

         add_filter( 'cron_schedules', 'wpdocs_add_cron_interval' );
         function wpdocs_add_cron_interval( $schedules ) {
              $options = get_option( 'wpdocs_custom_interval' );
              $interval = ( ! empty( $options ) ? absint( $options ) * HOUR_IN_SECONDS : DAY_IN_SECONDS );
    
              $schedules['wpdocs_interval'] = array(
                   'interval' => $interval,
                   'display' => esc_html__( 'Custom Interval' )
              );
    
              return $schedules;
        }
    

    Using class based approach

         add_filter( 'cron_schedules', array( $this, 'add_cron_interval' ) );
    
        /**
         * Adds custom time intervals for the cron.
         */
        private function add_cron_interval( $schedules ) {
              $options = get_option( 'wpdocs_custom_interval' );
              $interval = ( ! empty( $options ) ? absint( $options ) * HOUR_IN_SECONDS : DAY_IN_SECONDS );
    
              $schedules['wpdocs_interval'] = array(
                   'interval' => $interval,
                   'display' => esc_html__( 'Custom Interval' )
              );
    
              return $schedules;
        }
    
  4. Skip to note 4 content
    Contributed by BogdanFix

    As a handy tip to save your time, I’d suggest to always hook to cron_schedules filter inside a major action that runs on each page load. Like init or wp_loaded. And also hook to that filter before your code using the custom schedule.

    For example:

    // function that registers new custom schedule
    
    function bf_add_custom_schedule( $schedules )
    {
        $schedules[ 'every_five_minutes' ] = array(
            'interval' => 300,
            'display'  => 'Every 5 minutes',
        );
    
        return $schedules;
    }
    
    // function that schedules custom event
    
    function bf_schedule_custom_event()
    {
        // the actual hook to register new custom schedule
    
        add_filter( 'cron_schedules', 'bf_add_custom_schedule' );
    
        // schedule custom event
    
        if( !wp_next_scheduled( 'bf_your_custom_event' ) )
        {
            wp_schedule_event( time(), 'every_five_minutes', 'bf_your_custom_event' );
        }
    }
    add_action( 'init', 'bf_schedule_custom_event' );
    
    // fire custom event
    
    function bf_do_something_on_schedule()
    {
        // your code...
    }
    add_action( 'bf_your_custom_event', 'bf_do_something_on_schedule' );
    

    This approach guarantees that your custom schedule is always registered before it is used in wp_schedule_event function. And saves you from nasty "invalid_schedule":"Event schedule does not exist." error.

    Cheers :)

You must log in before being able to contribute a note or feedback.