7

I'm trying to setup a cron to run every hour, it works fine on my local vagrant box, but it doesn't seem to schedule properly on aws(elastic beanstalk). Here's the code:

register_activation_hook(__FILE__, 'my_activation');
add_action('my_hourly_event', 'do_this_hourly');

function my_activation() {
    wp_schedule_event(time(), 'hourly', 'my_hourly_event');
}

function do_this_hourly() {
    another_function();
}

register_deactivation_hook(__FILE__, 'my_deactivation');

function my_deactivation() {
    wp_clear_scheduled_hook('my_hourly_event');
}

is something wrong with this, or is something else at play?

I have w3 total cache installed both locally and on aws, so I don't think that would be to blame, as I've heard people mention it.

Thanks.

  • 2
    The WordPress cron API (by default) relies on a HTTP hit - if your setup is aggressively cached, WordPress might never actually be firing (or at least not frequent enough) to trigger cron. You might want to read into how to "properly" set up cron in WordPress. – TheDeadMedic Sep 1 '15 at 13:22
  • 1
    Thanks for the quick reply! I've started reading the link you provided, but I must note that the cron is never scheduled, it doesn't output when executing the following: $cron_jobs = get_option( 'cron' ); print_r($cron_jobs);, whereas locally, that same snippet shows me what I'm looking for. – aabreu Sep 1 '15 at 13:25
  • Do you have debugging enabled? – TheDeadMedic Sep 1 '15 at 13:27
  • Locally yes, not on the server. – aabreu Sep 1 '15 at 13:32
  • Turn it on and activate the plugin, see if you get any errors. – TheDeadMedic Sep 1 '15 at 13:41
0

You may use these steps to get it to work:` 1. Setup an schedule

function my_schedule( $schedules ) {
    $schedules['hourly'] = array(
        'interval' => 60 * 60,
        'display' => __( 'One houre' )
    );
    return $schedules;
}
add_filter( 'cron_schedules', 'my_schedule' );

2. Register the activation hook

register_activation_hook( __FILE__, 'my_activation' );

function my_activation() {
    $timestamp = wp_next_scheduled( 'my_hourly_event' );
    if( false == $timestamp ){
        wp_schedule_event( time(), 'hourly', 'my_hourly_event' );
    }
}

3. Add action on the event

function do_this_hourly() {
    // The cron functionality and rest of your code here
}
add_action( 'my_hourly_event', 'do_this_hourly' );
0

As a general principal, you shouldn't do anything that requires an 'add_action' after the plugin activation hook. This is because WP loads and runs all plugins and THEN runs the new added one, and then does a re-direct. You have to set a DB option and hook into that. Here is the discussion from the CODEX: https://codex.wordpress.org/Function_Reference/register_activation_hook

Try doing this outside of the activation hook and see what happens.In other words, PHP runs through the whole Wordpress routine on each browser request. When you 'activate' a plugin, you actually fire two page requests to the server. This type of activity properly goes in the 2nd page request, which is the re-direct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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