Site icon DsgnWrks

How To Use WordPress Transients to Increase Your Site’s Page Speed

This tutorial is built-in to the snippet. Drop this snippet in the functions.php file to see it in action, and play around with it to see how it works.

You’ll notice the “add_action( ‘all_admin_notices’….” hook. That will display this transient in the notice section at the top of your WordPress dashboard. I usually use that hook If I want to do some quick testing.

<?php
add_action( 'all_admin_notices', 'testing_testing_testing' );
function testing_testing_testing() {
    echo '<div id="message" class="updated">';

        get_your_loop_no_trans();
        get_your_loop(); // get_your_loop( true ); to not use transient

    echo '</div>';

}

// BEFORE TRANSIENTS:
function get_your_loop_no_trans() {

    $stuff = array(
        array( 'title' => 'thing 1', 'desc' => 'a description' ),
        array( 'title' => 'thing a', 'desc' => 'another description' ),
        array( 'title' => 'thing 2', 'desc' => 'a third description' ),
        array( 'title' => 'thing b', 'desc' => 'a ~fancy~ description' ),
        array( 'title' => 'one more thing', 'desc' => 'one more description' ),
    ); // your object or array to loop through

    // Your loop here:
    ?>
    <dl><h3>This is a definition list</h3>
    <?php
    foreach ( $stuff as $key => $thing ) {
        ?>
        <dt>This is <?php echo $thing['title']; ?><dt>
        <dd>Description: <?php echo $thing['desc']; ?><dd>
        <?php
    }
    ?>
    </dl>
    <?php
}

// AFTER TRANSIENTS:
function get_your_loop( $delete = false ) {

    if ( $delete == true ) {

        // delete the transient if the $delete flag is set to true.
        delete_transient( 'your_transient_name' );
        $data = false;

    } else {
        $data = get_transient( 'your_transient_name' );
    }

    if ( $data === false ) { // if we haven't found a transient, do this

        // Will tell you in your source code that the transient is NOT being used
        $transuse = "\n<!-- not using transient -->\n";

        $stuff = array(
                    array( 'title' => 'thing 1', 'desc' => 'a ' ),
                    array( 'title' => 'thing a', 'desc' => 'another ' ),
                    array( 'title' => 'thing 2', 'desc' => 'a third ' ),
                    array( 'title' => 'thing b', 'desc' => 'a ~fancy~ ' ),
                    array( 'title' => 'one more thing', 'desc' => 'one more ' ),
        ); // your object or array to loop through

        // start building our $data variable
        $data = '<dl><h3>This is a definition list</h3>';

        // Your loop here:
        foreach ( $stuff as $key => $thing ) {

                    // continue building our $data variable
                    $data .= '<dt>This is '. $thing['title']. '<dt>'.
                    '<dd>Description: '. $thing['desc']. '<dd>';
        }
        // This wraps up our $data variable
        $data .= '</dl>';

        // if our $delete flag isn't set, set our transient!
        if ( $delete != true ) set_transient( 'your_transient_name', $data, 86400 ); // time in seconds: 86400 is 24hrs

    } else { // This means we HAVE found a transient

        // Will tell you in your source code that the transient is being used
        $transuse = "\n<!-- using transient -->\n";

    }

    // Display our $data, (whether it's a transient) or not and wrap it in the $transuse html comments

    // in order to tell on the front-end if the transient is being used (view source)
    echo $transuse . $data . $transuse;

    // if $delete is not set to true, than only the first viewer of the "get_your_loop()" will endure

    // the query/loop etc. for subsequent users in the next 24 hours, it will be a lightning fast option grab.
}

Using transients have become a good habit for me. Anytime you’re creating a custom query, it’s a good idea to wrap it in a transient. Once you get a little more advanced you can start triggering the transient to delete from other places in your code, like on the ‘save_post’ hook so that the information is always up-to-date.

In the comments, let me know if you have any questions.

Exit mobile version