最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论