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

events - How to test wp_cron?

programmeradmin4浏览0评论

This is kind of a stupid question...

I scheduled a action to run every hour:

if(!wp_next_scheduled('my_hourly_events'))
  wp_schedule_event(time(), 'hourly', 'my_hourly_events');

add_action('my_hourly_events', 'the_function_to_run');

function the_function_to_run(){
   echo 'it works!'; 
}

How can I test if this works without waiting an hour? :)

I tried adding wp_clear_scheduled_hook('my_hourly_events'); before this code and adding wp_cron() after, but I don't see my function running...

edit:

ok, I added a trigger_error() inside my function, checked out the apache error log, and it's there :)

So now I'm even more confused:

  • How can the wp-cron run in the background? because apparently that's what happens if I see no output...

  • this doesn't seem to work in a object context; why?

This is kind of a stupid question...

I scheduled a action to run every hour:

if(!wp_next_scheduled('my_hourly_events'))
  wp_schedule_event(time(), 'hourly', 'my_hourly_events');

add_action('my_hourly_events', 'the_function_to_run');

function the_function_to_run(){
   echo 'it works!'; 
}

How can I test if this works without waiting an hour? :)

I tried adding wp_clear_scheduled_hook('my_hourly_events'); before this code and adding wp_cron() after, but I don't see my function running...

edit:

ok, I added a trigger_error() inside my function, checked out the apache error log, and it's there :)

So now I'm even more confused:

  • How can the wp-cron run in the background? because apparently that's what happens if I see no output...

  • this doesn't seem to work in a object context; why?

Share Improve this question edited Apr 10, 2011 at 23:13 onetrickpony asked Apr 10, 2011 at 21:27 onetrickponyonetrickpony 13.6k7 gold badges59 silver badges87 bronze badges 3
  • ok I found out the wp-cron.php is being executed with wp_remote_post(). that explains everything... – onetrickpony Commented Apr 10, 2011 at 23:33
  • By "object context", do you mean your callback is like array( &$this, 'my_method_name' )? That will indeed not work because the function name is stored in the database to be executed later. &$this refers to a specific object, not a class name, and this object will not exist at the next request when the cron job is executed. A static class function should work. – Jan Fabry Commented Apr 11, 2011 at 9:34
  • yes, I had to make my function static. I didn't think wp would remotely run a local script as "cron" – onetrickpony Commented Apr 11, 2011 at 17:05
Add a comment  | 

2 Answers 2

Reset to default 22

My favorite plugin for that is Core Control which has very nice module for display of what is going in the cron - which events are set up, when are they next firing, etc.

On getting your hands dirty level see _get_cron_array(), which returns internal stored data for cron events (top level of keys are timestamps).

wp-cli is another way:

Listing Events

> wp cron event list
+-------------------+---------------------+-----------------------+---------------+
| hook              | next_run_gmt        | next_run_relative     | recurrence    |
+-------------------+---------------------+-----------------------+---------------+
| wp_update_plugins | 2020-04-14 08:11:38 | 7 hours 24 minutes    | 12 hours      |
| wp_update_themes  | 2020-04-14 08:11:38 | 7 hours 24 minutes    | 12 hours      |
| wcsc_prime_sites  | 2020-04-14 17:00:55 | 16 hours 13 minutes   | 1 day         |
+-------------------+---------------------+-----------------------+---------------+

Schedule an Event

> wp cron event schedule wp_update_plugins "now +5 seconds"
Success: Scheduled event with hook 'wp_update_plugins' for 2020-04-14 00:43:54 GMT.

Then you can visit the front-end of the site and refresh a couple times to make sure it's triggered. Then run list again to see that it's no longer scheduled.

Run Directly in Terminal

> wp cron event run wcsc_prime_sites
Executed the cron event 'wcsc_prime_sites' in 0.805s.
Success: Executed a total of 1 cron event.

One quirk to be aware of is that wp-cli runs in the wp-admin context, but WP Cron runs on the front end. That usually isn't a problem, but when you're writing jobs, make sure that you require() any wp-admin/includes files that your code expects to be loaded.

If you don't, then the job will work fine in wp-cli, but will produce a fatal error when running normally.

Other commands

Run wp help cron for more details.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论