I’d like to execute some code everytime my wordpress cron runs, regardless of the timeframe. Is it possible to create an add_action for this? I looked in the filter list but I couldn’t find anything relevant.
Thanks!
I’d like to execute some code everytime my wordpress cron runs, regardless of the timeframe. Is it possible to create an add_action for this? I looked in the filter list but I couldn’t find anything relevant.
Thanks!
You must be logged in to post a comment.
Basically every page load looks to see if there is anything scheduled to run, so by that logic the cron is checked and can possibly run on every page load.
I can only presume you want to schedule something to run every so often. If that’s the case you need to look at wp_schedule_event()
Below is the code to get some code to run ever hour:
If you want to fire your own code when someone else’s cron has run then you need to tie into their action that is fired. So lets take the above code is someone else’s plugin and you want to fire your code every time theirs is ran.
Well they have created an action like this:
add_action('my_hourly_event', 'do_this_hourly');
so all you need to do is piggy back your function onto this:
After this remark from the OP:
I do not know if the W3 Total Cache plug-in not automatically flushes the cache when posts are published. I believed it did. If it does not – then in this intance I would hook onto post transition hook (see related question), and flush the cache then. This way, whenever a post changes status (via wp-cron or otherwise) – the cache is updated to the reflect this.
Orignal Problem
In response to the original question: firing a function whenever wp-cron is launched – I don’t think this is possible programatically – and the solution given by @Brady is probably the closest that you’ll get to doing that.
I recently had to do the same thing with w3 total cache, here is how I implemented it (simplified to answer the OP question)
To clear the page cache only when a future scheduled post actually changes it’s status to publish, the hook ‘publish_future_post’ is recommended. Add the code snippet to your functions.php.
Don’t use hook ‘transition_post_status’ which is executed, too. My debugging did show that sometimes the necessary class ‘W3_PgCacheFlush’ from the W3TC plugin can not be instantiated using this hook.
For someone who arrives here, there is a way to execute some code each time the cron URL is called and it can be simply done with the following code:
Where $_SERVER[‘REQUEST_URI’] probably can be omitted but is not dangerous.