I'm new to using transients. Is this the proper format to create a transient and have it pull from the DB instead of using the http api?

I have standardized my snippet so others can double check their code as well...

function google_transient() {

    $url = 'http://www.google.com';
    $the_whole_body = wp_remote_retrieve_body( wp_remote_get($url) );

    $transient_name = 'google';

    // Get any existing copy of our transient data
    if ( false === ( $transient_name = get_transient( $transient_name ) ) ) {

        // It wasn't there, so regenerate the data and save the transient
         set_transient( $transient_name, $url, 60*24); // 24 hour cache          

    return $the_whole_body;

1 Answer 1


No quite: Get the transient’s content first, then do the expensive work to fetch the external resource.

function google_transient() {
    $transient_name = 'google';
    $content        = get_transient( $transient_name );

    // done
    if ( $content )
        return $content;

    $url     = 'http://www.google.com';
    $content = wp_remote_retrieve_body( wp_remote_get($url) );

    set_transient( $transient_name, $content, DAY_IN_SECONDS ); // 24 hour cache          

    return $content;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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