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.
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.
Share Improve this question edited Sep 1, 2015 at 13:14 aabreu asked Sep 1, 2015 at 13:08 aabreuaabreu 2211 silver badge5 bronze badges 9- 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 Commented Sep 1, 2015 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 Commented Sep 1, 2015 at 13:25
- Do you have debugging enabled? – TheDeadMedic Commented Sep 1, 2015 at 13:27
- Locally yes, not on the server. – aabreu Commented Sep 1, 2015 at 13:32
- Turn it on and activate the plugin, see if you get any errors. – TheDeadMedic Commented Sep 1, 2015 at 13:41
2 Answers
Reset to default 1As 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/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.
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' );
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745054532a4608582.html
评论列表(0条)