Scheduling a cron job from a function that is ran outside functions.php?

I have a very simple function and hook registration in my functions.php, like this:function do_this_thingy(){ I am sen

I have a very simple function and hook registration in my functions.php, like this:

function do_this_thingy(){
   // I am sending myself some simple admin report mails, tested & works
}
add_action( 'mail_event_hook', 'do_this_thingy');

Then, in a page template I have a row:

wp_schedule_single_event( time() + 600, 'mail_event_hook' );
// expected: to receive message in ten minutes time. Result: no message :(

What might be going wrong?

Edit: Answers to questions:

  1. Is this on a local server? No, this is a live WP site of mine.
  2. How do I know that emails can be sent in the first place? The code that I use to send emails works perfectly if I simply run it from functions.php or another .php file on the server. I receive the email every time I try.
  3. Am I sure the CRON job fired? I have the plugin WP Crontrol and it shows no scheduled task / cron job. I assume thus that the answer is no.
  4. Are there PHP errors in the logs? I can take a look at this with debug logging enabled later today. Will update.

I have a very simple function and hook registration in my functions.php, like this:

function do_this_thingy(){
   // I am sending myself some simple admin report mails, tested & works
}
add_action( 'mail_event_hook', 'do_this_thingy');

Then, in a page template I have a row:

wp_schedule_single_event( time() + 600, 'mail_event_hook' );
// expected: to receive message in ten minutes time. Result: no message :(

What might be going wrong?

Edit: Answers to questions:

  1. Is this on a local server? No, this is a live WP site of mine.
  2. How do I know that emails can be sent in the first place? The code that I use to send emails works perfectly if I simply run it from functions.php or another .php file on the server. I receive the email every time I try.
  3. Am I sure the CRON job fired? I have the plugin WP Crontrol and it shows no scheduled task / cron job. I assume thus that the answer is no.
  4. Are there PHP errors in the logs? I can take a look at this with debug logging enabled later today. Will update.
Share Improve this question edited Dec 3, 2019 at 14:04 Jussi asked Dec 2, 2019 at 15:24 JussiJussi 234 bronze badges 2
  • 1 Is this on a local server? How are you confirming that your server is capable of sending emails? Are you sure the cron job fired? Or that it fired but the email part is what failed? Is there a message in your PHP error log? Please edit your question with those details, even if the answer is that you don't know – Tom J Nowell Commented Dec 2, 2019 at 16:08
  • @TomJNowell Thank you for the questions. I added the things you asked for. – Jussi Commented Dec 4, 2019 at 10:47
Add a comment  | 

1 Answer 1

Reset to default 0

Solved, although in a pretty poor manner.

If I move the scheduler function

wp_schedule_single_event( time() + 600, 'mail_event_hook' ); 

into functions.php it starts working. Not sure why, but this is the curse of self taught developers.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744933138a4601857.html

相关推荐

  • Scheduling a cron job from a function that is ran outside functions.php?

    I have a very simple function and hook registration in my functions.php, like this:function do_this_thingy(){ I am sen

    2天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信