Run a cron job (or similar) in the background of WP after post update/create

I would like to run a filter on post content after it has been created or updated.

I would like this to occur after the user has submitted the post, as it may be a bit of a lengthy process (a find/replace to search for glossary terms and replace them with links).

Read More

What’s the most efficient and reliable way to achieve this?

Thanks in advance,

Related posts

Leave a Reply

3 comments

  1. Alright, here’s some code I just whipped up. Completely untested, just wrote it right off the cuff…so don’t expect it to work 100% when you drop it in, but the core concept is there, as is a decent amount of the legwork

    add_action( 'my_filter_posts_content', 'my_filter_content' );
    add_action( 'save_post', 'my_set_content_filter' );
    
    if( !wp_next_scheduled( 'my_filter_posts_content' ) ) {
        wp_schedule_event( time(), 'hourly', 'my_filter_posts_content' );
    }
    
    function my_filter_content() {
        //check to see if posts need to be parsed
        if( get_option( 'my_updated_posts' ) == false )
            return false;
    
        //parse posts
        $ids = unserialize( get_option( 'my_updated_posts' ) );
        foreach( $ids as $v ) {
            YOUR_FUNCTION_HERE( $v );
        }
    
        //make sure no values have been added while loop was running
        $id_recheck = unserialize( get_option( 'my_updated_posts' ) );
        my_close_out_filter( $ids, $id_recheck );
    
        /*
        once all options, including any added during the running of what
        could be a long cronjob are done, remove the value and close out
        */
        delete_option( 'my_updated_posts' );
        return true;
    }
    
    function my_set_content_filter( $post_id ) {
        //get the previous value
        $ids = unserialize( get_option( 'my_updated_posts' ) );
    
        //add new value if necessary
        if( !in_array( $post_id, $ids ) ) {
            $ids[] = $post_id;
            update_option( 'my_updated_posts', serialize( $ids ) );
        }
    }
    
    function my_close_out_filter( $beginning_array, $end_array ) {
        $diff = array_diff( $beginning_array, $end_array );
        if( !empty ( $diff ) ) {
            foreach( $diff as $v ) {
                YOUR_FUNCTION_HERE( $v );
            }
        }
        my_close_out_filter( $end_array, unserialize( get_option( 'my_updated_posts' ) ) );
    }
    
  2. Please see my answer here. It’s a proof-of-concept for running a native crontab in Linux with your WP installation.

    As for WP-Cron functionality, beware of these caveats:

    • WP-Cron is a pseudo cron that is runs when WP is loaded. WP checks if a WP-Cron is scheduled or behind schedule to run and then executes the cron script.
    • If there is not adequate traffic, your cron might run really late.
    • Scheduling a WP-Cron to run during peak hours might cause some performance issues if it’s a large, intensive script.

    If you choose to go the WP-Cron route, here’s a great article to show you how to use it. You can also check out the WP functions in the Codex here.

    I prefer the reliability of a Linux crontab, especially for integral, heavy-weight cron scripts. For extremely lightweight scripts, I do use WP-Cron at times.

    Hope this helps!

  3. Would the save_post action work for you?

    <?php
    add_action( 'save_post', 'wp_save_post' );
    function wp_save_post()
    {
        // do stuff
    }
    

    You might also consider edit_post or publish_post

    You can view other actions in the Codex