• Hi all –

    I’m trying to set up a simple recurring event using the WordPress cron functions.

    By using wp_next_scheduled(‘my_scheduled_event_name’) and comparing this value to time(), I see that WordPress is correctly scheduling the event (and rescheduling the next recurrence as each scheduled time is reached).

    However, the actions that I’ve attached to the event hook never run.

    Here are the relevant bits of my code. First, my plugin adds an “Every 5 Minutes” schedule to the default cron schedules (this appears before any other code in my main plugin file):

    add_filter( 'cron_schedules', 'filter_schedules' );
    
    function filter_schedules( $schedules ) {
    	$schedules['5min'] = array(
    		'interval' => 300,
    		'display' => __('Every 5 Minutes')
    	);
    	return $schedules;
    }

    Upon activation of the plugin, I schedule a recurring event:

    wp_schedule_event( time() + 300, '5min', 'cron_5min' );

    Again, all of this appears to be working properly.

    Here’s what isn’t working:

    add_action( 'cron_5min', 'my_cron_function' );
    
    function my_cron_function() {
    	die( 'in my_cron_function()' ); // this never happens
    	someOtherStuff(); // if I remove the die() above, this never happens either
    }

    Any clues? I’ve gotten the WordPress cron stuff to work before, but it’s always extremely fiddly, and I never really understand why things are (or aren’t) working.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter immersionactive

    (@immersionactive)

    I should add that the last code excerpt is run on every page load, in the main plugin file – not as part of the activation function.

    Thread Starter immersionactive

    (@immersionactive)

    Also, has_action(‘cron_5min’, ‘my_cron_function’) returns true.

    So it looks like the callback is being properly registered to the hook. It’s just not running for some reason.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Cron events being scheduled correctly, but associated actions aren't being run’ is closed to new replies.