Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From \$16.50/m

# Working With Changing, Cacheable Data? WP-Transients Has Got Your Back!

Changing data is ever present in a WordPress-powered site. Be it a new post, an added comment, or an updated tweet for the front page, the status of a blog is constantly being altered. Since such data is often useful to a visitor, there comes a need to display it. This, however, is plagued by the fact that dynamic information—especially from external sources—results in slower, burdened sites. When it comes to these issues, the WordPress Transient API has you covered with its robust caching. The following screencast will demonstrate exactly how to take advantage of its features.

## Step 1 Get the Transient by Name

Access a transient by passing a name to the get_transient() function. Don't worry; even though you haven't created one yet, this is still the first step! For our example, we'll be getting a Twitter follower count.

## Step 2 Check if the Transient Has Expired

If the return value of get_transient() is false, the transient has either expired (based on the time parameter explained in step 3) or was never set in the first place:

## Step 3 If Expired, Set the Transient

When a transient expires, it needs to be reset; in other words, the data needs to be refreshed. To accomplish this, first retrieve the new data. In this case, get the follower count via the Twitter API as per the video:

Now store it by using the set_transient() function. Note that this requires a name, value, and expiry time in seconds as parameters. After this time has elapsed, get_transient() will return false and trigger this step again:

## Step 4 Put it All Together

Combining this into one function yields:

The follower count not only is available for use anywhere on your site, but it is also efficiently cached each day to curb load times.

## Final Code From the Video

The final code from the screencast—with a few modifications—is included below: