Using WordPress Transients for caching demands no extra paid WordPress Plugins – you can simply add few lines of PHP codes to cache certain queries. We have written about WordPress Transients in quite good details. That article is a minimum need to understand what we are going to write in this article.
WordPress 3.8 and Using WordPress Transients For Caching
As practically you can see, WordPress 3.8 has been visually and on coding has changed a huge, the default free theme can easily compete with a very nicely coded paid Theme. With time, it is more likely that the market of premium WordPress Themes will not exist anymore. The reason of existing premium WordPress Themes is because of the Framework – like provided by Genesis or Thesis. With time, the distance will gradually decrease and all of the advanced users need to understand the basics of how a Child Theme can be created. You can search on this website for the same. We want to avoid becoming offtopic and remain on the point – Using WordPress Transients For Caching.
The basic advantage of the situation of using an universal Theme Framework is the ability to share and use the required codes, snippets from and with a large number of users. With time, WordPress community really took important steps for making WordPress dot ORG better – it is not that easy to understand, unless one is related to Core Development of WordPress.
---
With many plugin, software generated WordPress Themes; the functions.php of the current Theme or Child Theme might not work properly in the way we want.
Using WordPress Transients For Caching
Many theoretical things are written, but without practical working example on how on using WordPress Transients for Caching, practically the sayings are valueless for a relatively new to WordPress user. Example can be :
1 2 3 4 5 6 | $transient_key = 'my-transient-key'; $data = get_transient( $transient_key ); if ( $data == '' ) { $data = get_posts( array( 'posts_per_page' => -1 ) ); set_transient( $transient_key, $data, 60 * 60 * 24 ); } |
If one add these snippets after changing the example values like my-transient-key on Theme’s or child Theme’s functions.php file, they will override any cache plugin settings. You need to adjust or fully deactivate some plugin to avoid a kind of conflict out of double caching. Now, in a WordPress Web Page, what usually we have ‘changing’ values ? You can think about the widgets – Google Plus or Twitter on this website. Within one hour, the change of followers really does not matter much. Recent Post really do not change hourly, unless new post is published.
We have forgotten who originally wrote this snippet for Twitter, but this a great example for using WordPress Transients :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | function my_followers_count($screen_name = 'user-name'){ $key = 'my_followers_count_' . $screen_name; // Seek for a cached version $followers_count = get_transient($key); if ($followers_count !== false) return $followers_count; else { // Send query to Twitter, // depreciated old API used for example, refer to API 1.1 docs from Twitter $response = wp_remote_get("http://api.twitter.com/1/users/show.json?screen_name={$screen_name}"); if (is_wp_error($response)) { // In case Twitter Server is down return get_option($key); } else { // Parse the body and json_decode it $json = json_decode(wp_remote_retrieve_body($response)); $count = $json->followers_count; // Store the result in a transient set_transient($key, $count, 60*60*24); update_option($key, $count); return $count; } } } |
We have used the PHP snippet without normal indentation, one can add indentation for cleaner coding.