0

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?

0

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
  • 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! – Duncan Edwards Sep 17 '15 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. – TheDeadMedic Sep 17 '15 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? – Duncan Edwards Sep 17 '15 at 12:31
  • be certain that add_action is being called every time the php file is loaded, not just when calling wp_schedule_single_event. Wordpress doesn't save which functions are called for an action so it has to be redeclared on every page load. – user3338098 Dec 6 '18 at 19:14

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.