Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Introduction to the Transients API in WordPress

Posted on January 30, 2012 by Pippin in Advanced, Tutorials, Video Tutorials, WordPress Database, Writing Plugins 33 Comments
Home» Tutorials » Advanced » Introduction to the Transients API in WordPress
Tweet
Love It - 0

The Transients API in WordPress is a simple method for storing cached data in the database. It allows us to take resource-intensive queries and store them in temporary caches to improve performance. Transients are temporary, meaning that after a specified amount of time, the cached data will be deleted and re-cached, or updated.

This quick demonstration will walk you through how to use transients in your WordPress development.

Working with transients is quite simple. There are just three functions that you really need to know about:

  1. get_transient() – Used to retrieve the cached data
  2. set_transient() – Used to store data in a cache
  3. delete_transient() – Used to delete cached data

The function below will demonstrate a very, very basic usage of transients:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function pippin_transient_demo() {
 
	// get the cached data
	$data = get_transient('sample_trans');
 
	// check to see if data was successfully retrieved from the cache
	if( false === $data ) {
		// do this if no transient set
		$data = 'This is the data stored in the transient';
		// store the data and set it to expire in 10 seconds
		set_transient('sample_trans', $data, 10);
	}
 
	// this will output "This is the data stored in the transient" regardless of whether a the data was retrieved from the cacue or not.
	echo $data;
 
}
add_action('admin_notices', 'pippin_transient_demo');

While the example I just showed you is extremely simple (and silly), the implementation in a realistic situation is the same.

If you would like a more complete example that demonstrates a realistic usage, then check out Michael Martin’s great tutorial on listing recent comments with the transients API.

Update 02-01-2012:

Piet (in the comments) pointed out a really great article that walks through quite a few great examples of how to use transients in real world examples. Check it out the article on speckyboy.

Tweet Follow @pippinsplugins
cache, performance, transients

33 comments on “Introduction to the Transients API in WordPress”

  1. paul says:
    January 30, 2012 at 6:16 pm

    nice and simple example, thanks! I suppose it can be useful to get data from external domains too, like a twitter follower count?

    one thing: the sound of your keyboard typing is really loud and distracting, sounds like your hitting a drum :D

    Reply
    • Pippin says:
      January 30, 2012 at 6:37 pm

      Yep, that’s a perfect example of what it could be used for. Michael Martin (who I linked to at the bottom) actually did a tutorial on exactly that.

      Yeah, still working with my new mic and figuring out settings and such. It probably doesn’t help that my keyboard is on a hard wood desk :P

  2. Piet says:
    January 31, 2012 at 2:17 am

    thanks for this very clear tutorial, it makes a lot of sense at last!

    I also have a question regarding all these transients feeds stored in the database. Last night I was switching a site over to another server and as the site already exists for a couple of years, it has heaps and heaps of these transients stored in the database. Mostly in the options table and many are feeds of installed plugins, wordpress news feeds, wordpress plugins news feeds, etc.
    Is it safe to just delete that whole bunch in order to make the database a little smaller or does it not matter at all or can it not be deleted just like that?

    Reply
    • Pippin says:
      January 31, 2012 at 9:50 am

      You should be able to delete them no problem, though some will likely just be recreated by your active plugins / theme.

    • Piet says:
      January 31, 2012 at 11:53 am

      thx for the reply. yeah I am wondering why they would be recreated. A feed normally takes only the last 10 posts, correct? Or in this case it takes everything since the plugin was installed or sth?

    • Pippin says:
      January 31, 2012 at 1:39 pm

      It could be a variety of things. Since they’re created through plugins, anything could be happening.

  3. Steven Bradley says:
    February 1, 2012 at 12:28 am

    I’ll add my thanks for the clear and understandable tutorial.

    You mentioned how transients are very useful for expensive queries. I get the concept, but I’m not so clear on when queries are more or less expensive. Usually when I write queries I’m just happy to get back what I wanted. However, I can’t say I know how to optimize those queries or recognize when they are on the expensive side.

    Any chance you could offer a tutorial or a series on queries?

    Reply
    • Pippin says:
      February 1, 2012 at 9:22 am

      Sure! I can definitely look into doing some tutorials on queries.

  4. Piet says:
    February 1, 2012 at 11:04 am

    Just did some more reading on transients form an article that was published at the end of last year, might be interesting for others too, so I thought I’d share it here: http://speckyboy.com/2011/12/14/website-speed-part-3-caching-wordpress/

    Reply
    • Pippin says:
      February 1, 2012 at 11:28 am

      Oh, thanks for the link. That’s a good article with tons of good examples. I’ll update the post with it.

  5. Michael Martin says:
    February 7, 2012 at 12:15 pm

    Awesome tutorial Pippin, your videos are really great for seeing the steps completely. Great resource, the transients API has to be one of my favorite features in WordPress as a developer these days. Comes in useful surprisingly often!

    Reply
    • Pippin says:
      February 7, 2012 at 12:27 pm

      Thanks, Michael! I’ve only just recently started using Transients, but I love what they can do!

  6. Sam C. says:
    March 14, 2012 at 5:36 am

    Very Good!
    I will start using this. :)

    Reply
    • Pippin says:
      March 14, 2012 at 12:20 pm

      Glad you like it, Sam.

  7. yellowhousedesign says:
    July 10, 2012 at 1:59 pm

    What’s your opinion on using this on a custom post type that does 3 separate queries? For example, I’d set the transient as ‘collection_pages_’.$post->ID and since there are three per post (in the post type), there’d be 3 x the amount of posts. It seems like a lot to store in the options table — but in your mind, do the performance gains outweigh the extra DB size?

    Reply
  8. yellowhousedesign says:
    July 10, 2012 at 2:32 pm

    Am now storing the three queries in an array — that better you think? :)

    Reply
  9. Pippin says:
    July 10, 2012 at 2:37 pm

    What do the queries look like?

    Reply
  10. yellowhousedesign says:
    July 10, 2012 at 2:55 pm

    http://pastebin.com/SPS4YM94 for example

    Reply
    • Pippin says:
      July 10, 2012 at 3:29 pm

      Yes, those are good to drop into a transient. I asked Otto about it and he said there was no problem with having literally thousands of these entries in the DB. On of the the sites I’ve worked on that has over 50K users, we’re using transients to cache info related to each individual user without any problems at all.

  11. yellowhousedesign says:
    July 10, 2012 at 3:47 pm

    Ah great! Yeah I noticed the number of queries go down after implementing – so I’m excited to add this onto other areas. Just wondering – according to that SpeckBoy article, you need to have WP_CACHE turned on in wp-config — I didn’t see a caching plugin from your screen-cast, are you currently using one?

    Also – a mental note for myself (not sure if this was covered in the video), but transients are not good for when your query is getting random posts – as they won’t be that random anymore ;)

    Thanks!

    Reply
  12. Pippin says:
    July 11, 2012 at 12:04 am

    Transients do not require WP_CACHE be turned on, but other caching methods in WP do.

    Reply
  13. yellowhousedesign says:
    July 11, 2012 at 8:12 am

    Ah thanks for clarifying – that part didn’t make sense to me on that other article. I’m thinking of rolling in Transients to single post views — and then what I could do is on-save (in the back-end) delete the transient (so it is always kept up to date if the content is ever re-edited) but have a decently long expiration time. Does that sound plausible/a good idea? Transients are pretty cool, but I’d hate to go over-board on that if what I’m thinking isn’t necessary with caching methods (or if this is just a better “cache within a cache”). Thanks!

    Reply
  14. Pippin says:
    July 11, 2012 at 12:22 pm

    That’s a great idea in fact. I’ve done that a couple of times using the “save_post” hook so that the contents are always up to date.

    Reply
  15. yellowhousedesign says:
    July 11, 2012 at 1:18 pm

    Nice, off I go! :)

    Reply
  16. Jason Manheim says:
    October 22, 2012 at 8:00 pm

    Any idea why this doesn’t work? https://gist.github.com/3935999

    If I delete the transient stuff it displays the count just fine.

    Reply
    • Jason says:
      October 22, 2012 at 8:15 pm

      Bah! Never mind. Looking at it for so long I didn’t notice I used follower_count in one place and subscriber_count in another.

      Thanks for the great intro.

    • Pippin says:
      October 22, 2012 at 9:04 pm

      That’s easy to do :)

  17. Dave Navarro says:
    February 9, 2013 at 9:09 pm

    I am guessing that the RSS feed reader in WordPress uses this mechanism for caching the feeds. I found info on the “wp_feed_cache_transient_lifetime” filter for globally setting the feed timeout length.

    However, when I look in the DB, it appears that WordPress actually sets this value for each individual feed, even though the filter mechanism doesn’t allow you to specify individual feeds. I’ve looked through the various RSS related files in wp-includes and I can’t seem to figure out the mechanism for naming the transients of each feed.

    One of the RSS feeds on a client site displays all of the articles for a given category. I’d like to flush the cache for that feed when a new article is posted. Otherwise, it takes up to an hour for the article to show up (I set the global timeout to 1 hour).

    Any ideas as to where I might look?

    Reply
    • Pippin says:
      February 11, 2013 at 8:43 pm

      I’ve never played with that filter so I can’t say for sure. I’d imagine if there isn’t a unique filter for each, then there is probably a way to detect which feed we’re working with from inside of the filter. Is there?

    • Dave Navarro says:
      February 12, 2013 at 8:25 am

      After doing a lot of research, I have discovered that fetch_feed saves the following in the wp_option table:

      _transient_timeout_feed_mod_XXXX
      _transient_timeout_feed_XXXX
      _transient_feed_mod_XXXX
      _transient_feed_XXXX

      where XXXX is an md5($url), so the following will delete the cache for a given feed:

      $key = md5(“your_feed.url”);
      delete_option( “_transient_timeout_feed_mod_{$key}” );
      delete_option( “_transient_timeout_feed_{$key}” );
      delete_option( “_transient_feed_mod_{$key}” );
      delete_option( “_transient_feed_{$key}” );

      The URL itself is not stored in the database if you call fetch_feed() directly. So there is no way to get a list of the feeds you’re using, unless you are using them through the RSS widget which does store the names of the feeds.

      Presuming you know the URL of your feed and you use md5() on it, you can find the timeout value for the feed and change that value. I’m guessing… I haven’t tested it yet.

    • Pippin says:
      February 12, 2013 at 8:18 pm

      That sounds correct and should work just fine. Let me know if it works once you test it out!

    • Dave Navarro says:
      February 12, 2013 at 11:14 pm

      Yeah it works. I was able to change the time-out value for internal RSS feeds (feeds that come directly from my client’s web site) to 1 minute and leave the external feeds coming from outside sites at 1 hour.

      What I need to do is filter calls to fetch_feed so that I can build an index of RSS feeds and store them in wp_options. Then I can create a plugin that allows admins to individually set feed time-out values. Not sure how to do it yet, but I’ll figure it out.

    • Pippin says:
      February 13, 2013 at 7:07 pm

      Great!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

Sorry, no related items found.

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes
  • Plugin Development 101 – Registering a Custom Post Type
  • Plugin Development 101 – Intro to Actions

Latest Tutorials

  • Test Your Plugins with RTL (0)

    Right-To-Left languages are those that...

  • Submitting Your First Pull Request to a WordPress Plugin on Github (5)

    Github is an extremely popular tool for managing WordPress plugins, and one...

  • Plugin Development 101 – Introduction to Adding Dashboard Menus (1)

    Adding new menus, both top level and sub level, to the WordPress Dashboard is a really common task for plugins...

Enter your email to receive automated updates when new posts are published

Latest Tweets

  • @corymiller303 I hope everything with the surgery went well!
    May 25, 2013
  • RT @strickland: To celebrate Memorial Day weekend @gittyapp is on sale through Monday. Now is the time to join in! http://t.co/vOAqksLLrN
    May 25, 2013
  • RT @GaryJ: Premium theme sellers: consider spending 30mins to add an rtl.css to your themes, to considerably open up your potential market.
    May 25, 2013

Topics

hook meta box Rémi Corson featured shortcodes campaign monitor add_options_page register_setting Sugar Event Calendar attachments add_shortcode wp_enqueue_script the_content image forms short codes Related posts login do_action authors mail chimp attachment plugin recent posts comments post types bbpress apply_filters short code taxonomies custom post type Ajax images gallery Stripe jquery taxonomy users widgets add_filter easy content types add_action widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2013 Pippin's Plugins