0

I am trying to create custom interval for cron schedule with given user input, but its not working.

Here is the code:

add_filter( 'cron_schedules', 'custom_cron_schedule' );
function custom_cron_schedule( $schedules,$int ) {
    $schedules['custom'] = array(
        'interval' => $int, // user input integer in second
        'display'  => __( 'Custom Interval' ),
    );

    return $schedules;
}

I tried to look at custom schedule via _get_cron_array() and found that interval is missing I also checked $int, it has 36000.

So what am I missing here OR am I not allowed to use arguments in this filter?

1 Answer 1

1

Note on input arguments:

You are using the filter callback function (FCB) with two input arguments, but the default is one.

In general you should replace:

add_filter( 'some_filter', 'some_filter_callback' );
function some_filter_callback( $arg1, $arg2 ) {

with something like:

add_filter( 'some_filter', 'some_filter_callback', 10, 2 );
function some_filter_callback( $arg1, $arg2 ) {

if you want two arguments.

You would also have to look at the apply_filters( 'some_filter', ... ) part to find the correct number of input arguments.

The problem:

When you check the source of wp_get_schedules() you find the following:

return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );

so there's no second $int argument passed on to the FCB.

The array that you return from your FCB is then merged with the default $schedules, where:

$schedules = array(
        'hourly'     => array( 'interval' => HOUR_IN_SECONDS,      'display' => __( 'Once Hourly' ) ),
        'twicedaily' => array( 'interval' => 12 * HOUR_IN_SECONDS, 'display' => __( 'Twice Daily' ) ),
        'daily'      => array( 'interval' => DAY_IN_SECONDS,       'display' => __( 'Once Daily' ) ),
    );

Hope this helps.

4
  • thanks for the detailed explanation, I tried to use following function but still no success, Please can you help me with a example for my problem ? CODE: function cron_interval($int){return array('custom'=>array('interval'=> $int,'display'=>'custom'))} Apr 12, 2014 at 16:13
  • You can use the Options API (set_option/get_option) or Custom User Meta (set_user_meta/get_user_meta) to store and retrieve the user input data. You could also consider wrapping your code into a class to make it easier.
    – birgire
    Apr 12, 2014 at 16:28
  • finally I found the problem, its variable scope, I am calling this function inside a class function so its not working but now I make that $int variable global so its working ...silly mistake by me...thanks for your help birgire Apr 12, 2014 at 17:00
  • 1
    yes it's the scope, but I don't recommend solving this by adding a global variable. If you are constructing a class, use for example $this->interval.
    – birgire
    Apr 12, 2014 at 17:07

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.

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