add_action to wp cron?

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!

Related posts

Leave a Reply

5 comments

  1. 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
    }
    
  2. 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.

  3. 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
    
    
    }
    
  4. 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();
        }
      }
    }
    
  5. 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:

    add_action( 'init', 'my_function_on_each_cron_call' );
    function my_function_on_each_cron_call() {
        if ( isset( $_GET[ 'doing_wp_cron' ] ) && $_SERVER['REQUEST_URI'] == '/wp-cron.php?doing_wp_cron' ) {
            // YOUR CODE GOES HERE
        }
    }
    

    Where $_SERVER[‘REQUEST_URI’] probably can be omitted but is not dangerous.