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 s
2

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.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *