Use wordpress transient api to make page loading faster

As we all know page speed is very important in web world. I would like to introduce you a very good practice in wordpress to use transient to reduce page loading time especially when high computational or resource consuming business requirement involved.

The API document is here: https://codex.wordpress.org/Transients_API

Compared to other cache mechanism, transient is easy and practical, it still stores result into database but allow you to set a expiry time, fair enough. No need to have more configurations on server/environment, just use it.

Ok, as usual, code first.

 

<?php //you can set the expiry time based on your reality 
if(false === ($output = get_transient('content'))){ 
    set_transient('content', $content, 12 * HOUR_IN_SECONDS); 
} 
echo $output; 
?>

That is it. Easy to go!