Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
    
What exactly are you trying to achieve? I don't think what you're asking is possible - but if we know what you are trying to do then there might be an alternative... –  Stephen Harris Apr 23 '12 at 9:19
    
What I want to do is clear my W3 Total Cache (flush_pgcache()) everytime a cron is run. –  FLX Apr 23 '12 at 9:32
    
To be clearer, I have scheduled posts in the future, and I want the cache to clear when the are published via the wp-cron trigger. There might be a better way? –  FLX Apr 23 '12 at 10:02
add comment

4 Answers

up vote 3 down vote accepted

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:

add_action('my_hourly_event', 'do_this_hourly');

// The action will trigger when someone visits your WordPress site
function my_activation() {
    if ( !wp_next_scheduled( 'my_hourly_event' ) ) {
        wp_schedule_event( current_time( 'timestamp' ), 'hourly', 'my_hourly_event');
    }
}
add_action('wp', 'my_activation');

function do_this_hourly() {
    // do something every 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:

add_action('my_hourly_event', 'my_flush_function');
function my_flush_function() {
    // Do your code here
}
share|improve this answer
    
Thanks for the answer, but I want it to specifically hook into the existing cron events, firing once the cron has been executed. What I want to do is clear my W3 Total Cache (flush_pgcache()) everytime a cron is run. –  FLX Apr 23 '12 at 9:32
    
OK I'm going to update my question to answer. Give me a minute. –  Brady Apr 23 '12 at 9:38
    
Ah thanks! To be clearer, I have scheduled posts in the future, and I want the cache to clear when the are published via the wp-cron trigger. There might be a better way? –  FLX Apr 23 '12 at 10:02
add comment

After this remark from the OP:

To be clearer, I have scheduled posts in the future, and I want the cache to clear when the are published via the wp-cron trigger. There might be a better way?

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.

 //Runs whenever a post changes status
 add_action('transition_post_status','wpse49927_transition_post_status',10,3);
 function wpse49927_transition_post_status($new_status,$old_status,$post){
     //Flush cache
     flush_pgcache();
 }

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.

share|improve this answer
add comment

I recently had to do the same thing with w3 total cache, here is how I implemented it (simplified to answer the OP question)

<?php
/**
* fac_all_crons allows you to attach a function to all wp-cron's registered in a WordPress site
* @author Russell Fair
* @uses wp_get_schedules to get a list of registered crons
*/
function fac_all_crons(){

    $schedules = wp_get_schedules();

    if ( !$schedules )
        return;

    foreach ( $schedules as $key ){

        if ( ! wp_next_scheduled( 'fac_cache_clear' ) ) {
            wp_schedule_event( time(), $key, 'fac_cache_clear' );
        }

    }

}
add_filter('init', 'fac_all_crons');

/**
* fac_cache_clear does the actual cache clearing
* @author Russell Fair
*/
function fac_cache_clear(){

    //do your cache clearing here


}
share|improve this answer
    
for clarity, I'm not sure if this would indeed execute on EVERY event, as the !wp_next_scheduled() might be true in some cases, you might try it with out the conditional check around the wp_schedule_event() function. –  rfair404 Aug 22 '12 at 16:05
add comment

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.

/**
 * This function clears the complete W3TC cache when a future post is published.
 * jot 11.09.12
 */
add_action('publish_future_post', 'wpse_49927_clear_w3tc_cache_on_future_to_publish');

function wpse_49927_clear_w3tc_cache_on_future_to_publish( $post_id) {
  if (function_exists('w3_instance') && class_exists('W3_PgCacheFlush')) {
    $w3_pgcache = & w3_instance('W3_PgCacheFlush');
    if (is_object($w3_pgcache) && method_exists($w3_pgcache, 'flush')) {
      $w3_pgcache->flush();
    }
  }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.