0

Background - I have created a shortcode in functions.php which adds a single random tag with it's link and description to a widget in my sidebar. This is my existing code...

function skips_get_random_tags() {
    $args = array('exclude' => '');
        $alltags = get_tags( $args );
        shuffle($alltags);
        $count=0;
        if ($alltags) {
            foreach($alltags as $tag) {
                $count++;
                return '<H5>Random App: <a href="'.get_tag_link($tag->term_id).'">'.$tag->name.'</a></H5><p class="random-tag-description skips-answer">'.$tag->description.'</p>';
        if( $count >0 ) break;
        }
    }
}

add_shortcode( 'random-tag', 'skips_get_random_tags' );

It works fine except that is runs every time the page is loaded (subject to a cache period) and also generates different results on each page because of this. I would like to schedule it to run once daiy and have been reading about Cron which seems to be the solution. Having reviewed the codec, I believe I need to edit my code as follows (seems to be just adding some CRON code to trigger the function above the code and linking it to the function)...

function prefix_activation() {
    wp_schedule_event( time(), 'daily', 'skips_daily_event_hook' );
}

add_action( 'skips_daily_event_hook', 'skips_get_random_tags' );
/**
 * On the scheduled action hook, run the function.
 */

    function skips_get_random_tags() {
    $args = array('exclude' => '');
        $alltags = get_tags( $args );
        shuffle($alltags);
        $count=0;
        if ($alltags) {
            foreach($alltags as $tag) {
                $count++;
                return '<H5>Random App: <a href="'.get_tag_link($tag->term_id).'">'.$tag->name.'</a></H5><p class="random-tag-description skips-answer">'.$tag->description.'</p>';
        if( $count >0 ) break;
        }
    }
}

add_shortcode( 'random-tag', 'skips_get_random_tags' );

I'm suspecting I'm going the long way round but I don't want to trigger a CRON that I can't stop - can anyone experienced with CRON comment on my approach and proposed code?

1

Use WordPress Transient API instead. This should be something like this (modified your existing shortcode function).

function skips_get_random_tags() {
    $transient_name = 'skips_random_tag';
    $transient_expiration = 60 * 60 * 24; // 1 day

    if ( false === ( $tag = get_transient( $transient_name ) ) ) { // tag isn't found in transient so get it from WP database
        $args = array('exclude' => '');
        $alltags = get_tags( $args );

        if(empty($alltags)){
            // no tag found show warning
            return false;
        }

        shuffle($alltags);   
        $tag = $alltags[0];

        set_transient( $transient_name, $tag, $transient_expiration ); // set the transient
    }

    return '<H5>Random App: <a href="'.get_tag_link($tag->term_id).'">'.$tag->name.'</a></H5><p class="random-tag-description skips-answer">'.$tag->description.'</p>';

}

add_shortcode( 'random-tag', 'skips_get_random_tags' );

Explanation

What is happening here is. When you first run the code WordPress will pull the tag from the data base and set the transient which expiration is 24 hours. If the code runs again in next 24 hours the tag saved in transient will be down until it expired. When it gets expired then WordPress will run the query again and save a new tag.

Code is not tested. I feel that if you are showing it on sidebar you can try creating widget instead of shortcode. But for both ways solution would be similar.

  • 1
    OK I've implemented the code and right now it appears absolutely spot on. The same random tag is displayed site wide which means it seems to be collecting the tag from the transient cache. I now need to wait 24 hours and see how it updates and then I will very happily mark the answer as resolved. Thank you for the detailed code and explanation. Very helpful for my own development. – Paul Brown Jun 16 '14 at 16:26
  • 1
    You can change the transient name and set the expiration to smaller value like 60 seconds to speedup the process ;) – Sisir Jun 17 '14 at 5:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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