wp_schedule_event won’t accept args

I’m trying to schedule event on hourly basis but it doesn’t work. Why, I can’t understand.

Here is a function that i use to schedule event.

Read More
add_action( 'scheduled_update_feeds', 'update_feeds', 10, 2 );

function scheduled_activation() {
    wp_schedule_event( time(), 'everyhour', 'scheduled_update_feeds', array( 0, 'everyhour' ) );
}
register_activation_hook( __FILE__, 'scheduled_activation' );

Per my understandings this should activate on plugin activation and pass schedule event based on my settings.

When I use it without array args it works, but I need to use args to pass them my scheduled function.

And this is the function

function update_feeds($ids = false, $timing = false) {
    do something
}

When i use it like this it works.

wp_schedule_event( time(), 'everyhour', 'scheduled_update_feeds' );

Even if I don’t use args in my update_feed function it doesn’t work either. The strange thing is that when I use single schedule it works.

like this

wp_schedule_single_event( time() + 10, 'scheduled_update_feeds', array($ids) );

So I don’t get it why it won’t work.

And I did defined ‘everyhour’ in my scheduled list.

add_filter( 'cron_schedules', 'my_schedule' ); 

$schedules['everyhour'] = array(
    'interval' => 60,
    'display' => __( 'Every hour', 'easy-video-grabber' )
);

I checked its existance with the Cron Schedules plugin and the schedule exists.

Related posts

3 comments

  1. This is not a fix for your problem, but it might lead you in the right direction to solving the issue. I would suggest checking out this plugin to possibly help you find out what is going on:

    WP Cron Control

    It offers a quick view of cron jobs scheduled throughout your site. I recently used it to resolve a problem occurring when scheduled emails were not being sent out.

  2. In the simplistic way. Register a filer, embed with action & use it. For quick example:

    Register an event:

    // Create new cron job filter
    add_filter( 'cron_schedules', 'cron_add_five_minute' );
    function cron_add_five_minute( $schedules ) {
      // Adds once weekly to the existing schedules.
      $schedules['five_minute'] = array(
          'interval' => 300,
          'display' => __( 'Five Minute' )
      );
      return $schedules;
    }
    

    Execute functions:

    add_action( 'wpfex_five_minute_event', 'wpfex_do_this_five_minute' );
    
    /**
     * On the scheduled action hook, run a function.
     */
    function wpfex_do_this_five_minute() {
        // Execute file every five minutes      
        // Do what ever you want to do
    }
    

    *Please note that wp cron jobs are different than usual linux cron jobs. WordPress internally run thread on when user hit the website. If no hit on website in a day then there is not cron job executed, because cron job thread is not running.

    For more information how cron jobs work in WP, check this quick reference guide

    Thanks

  3. I’ve had this problem before. What is happening (from the looks of it) is that you are setting up the action hook to be scheduled, but failing to make use of it.

    e.g.

    function update_feeds($ids = false, $timing = false) {
       do something
    }
    wp_schedule_event( time(), 'everyhour', 'scheduled_update_feeds' );
    

    In addition to this, you would need something like:

    add_action('scheduled_update_feeds', 'update_feeds', 10, 2);
    

    wp_schedule_event sets up an action to be run, so in addition to a function to do things, you need to add an action to be carried out on the action tag specified.

    • Your wp_schedule_event tells wordpress to do_action on scheduled_update_feeds on a regular basis
    • Your running function contains your processing to be done
    • You need to use add_action on scheduled_update_feeds to carry out your function.

    I’d also recommend checking to see if it exists before adding it:

    $next_run = wp_next_scheduled('scheduled_update_feeds');
    if ( $next_run ) {
        wp_unschedule_event( $next_run, 'scheduled_update_feeds');
    }
    $first_run = time();
    $result = wp_schedule_event( $first_run, 'everyhour', 'scheduled_update_feeds');
    

    Finally, I’d recommend using less generic names, especially for your processing function. There are probably plenty of people out there that will create an update_feeds function.

    Anywise, hope that helps.

Comments are closed.