1

I have created the following action:

add_action( 'test_action', 'test_action' );

function test_action() {
    error_log( 'test_action' );
}

And am calling it like this:

error_log( 'send_test_email_in_background:START' );

wp_schedule_single_event( time(), 'test_action' );

error_log( 'send_test_email_in_background:END' );

What I receive in the apache error log file is this:

[17-Sep-2015 11:09:35 UTC] send_test_email_in_background:START

[17-Sep-2015 11:09:35 UTC] send_test_email_in_background:END

This works exactly the same in development on my Windows PC (running WAMP) than it is on Production (running Linux in AWS). Also, if I schedule the single event 5 minutes in the future (time() + 300) it shows in my "Debug Bar Cron" plugin as being available to run in 5 minutes time and it appears to run just fine. So it appears that the CRON jobs are being created but for some reason they are not actually calling the action when running? Has anyone else had this problem or have an insight into it?

2 Answers 2

1

Because you're scheduling the event - even though it's set to run immediately (i.e. with time()), it won't fire until a second request.

So that log output is entirely expected. Try running it again, removing the code, then run once more - you should get:

[time] test_action
3
  • Thank you for your response. What do you mean exactly by "a second request"? How do I do that? And which code am I removing? Sorry, I just think I'm misunderstanding some aspect of your solution. Cheers! Sep 17, 2015 at 12:10
  • How good is your understanding of the WP cron API? When you schedule an event (of any kind), you're just pushing some arguments into a queue that's stored in the database. Those events won't actually ever fire until WordPress is loaded again - that's when it will pull in the queued events and decide if any need firing. Sep 17, 2015 at 12:14
  • OK, my understanding of WP cron API was very little until now. However I still can't get anything to fire under any context. I'm refreshing the pages of the site itself and the admin pages after the job is scheduled. The jobs are showing in a plugin that is used for showing CRON jobs and in the 'cron' option inside wp_options. Then disappearing (as they are being executed I imagine) but I'm still not getting any [time] test_action. I'm tempted to set this up as a proper linux cron job that is part of Linux... Maybe that might be a more robust solution? Sep 17, 2015 at 12:31
1

If you have used a template for WordPress admin functions, you may have been inclined to insert your cron jobs inside your admin class.

If, at the same time, your main code has an IF statement like this:

if (is_admin()) {
    foreach( glob ( PLUGIN_DIR . "/admin/*.php" ) as $filename ) {
       require_once( $filename );
    }
}

Then this will mean the admin files will not be loaded for cron. is_admin() returns true only for when admins are visiting the wp-admin pages.

If this is the case, you need to separate your add_action out to a file that always gets called by your plugin. You can still have your actual cron function inside your class if you wish, using a static function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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