• Categories
    • Tutorials
    • Beginners Guide
    • WordPress News
    • WordPress Security
    • Best WordPress Plugins
    • WordPress Themes
    • Product Reviews
    • WP Tips & Tricks
  • Guides
    • Start a Blog
    • Make a Website
    • WordPress Hosting
  • WordPress Hosting
    • A2 Hosting
    • HostGator
    • Bluehost
    • Cloudways
  • Managed Hosting
    • WPEngine
    • Rocket.net
    • WPX
    • Kinsta
  • Coupons
    • WPEngine
    • Flywheel
    • Cloudways
    • A2 Hosting
    • WPX Hosting
WordPress Tutorials

Back to the future with WordPress transients API

Last Updated on: July 26, 2013 Remi 1 Comment

Back to the future with WordPress transients API

If you use WordPress, you may have heard this word at least once: “transient”.

Good, you already achieved step one. But do you really know what transients are, and to use them?

I this quick post I’m going to explain what transients are and how to use them in your own developments. First of all, and this should be your first reflex, let’s go to the Codex.

First thing to note: transients is an API. And here is how the Codex describes it:

This page contains the technical documentation of the WordPress Transients API, which offers a simple and standardized way of storing cached data in the database temporarily by giving it a custom name and a timeframe after which it will expire and be deleted.

I guess it’s a bit clearer now isn’t it? In other words, the transient API is a perfect way to store your own variables for a limited period of time before they self-destruct. I already talked about transients on my website, explaining how to cache the result of a query using transients, but today we are going to make a more basic use of the API.

I recently did something pretty interesting: as a Codecanyon author I wanted to show on my “About me” page the number of happy clients I have. As Codecanyon is a marketplace, the number of happy clients increase every day, and I can’t update the value manually everyday. So, I decided to use the Envato API (Envato is the company behind many marketplaces such Codecanyon and Themeforest)  to retrieve my sales value, store this value in my database and refresh this value every 12 hours. This is exactly what transients API does.

I’m not going to explain the process to retrieve values from Envato API, so let’s say that the sales number is stored in a $envato_happy_clients variable. Here is what the code would be:


<?php
// Cache timeout in seconds
$CACHE_EXPIRATION = 60*60*12; // 12 hours

// Transient ID & cached data
$transient_id  = 'remi_happy_clients';
$happy_clients = get_transient( $transient_id );

// Is data already in local or new call to API
if ( !$happy_clients ) {

/*
Envato API call to retrieve sales number.
Let's assume the result is $envato_happy_clients
*/

$happy_clients = $envato_happy_clients;

// Set the transient - cache item data
set_transient( $transient_id, $happy_clients, $CACHE_EXPIRATION );

}

echo $happy_clients;
?>

And the result is something like you can see on my “About me” page. So, basically, we define in $CACHE_EXPIRATION the period we want to keep the cached data, we then place the content of the transient in $happy_clients variable and then we check if the value is set. If not we connecte to the Envato API to retrieve fresh information, otherwise we simply echo the happy customers value.

Well, this is it! It’s simple but powerful, and I really encourage you to have a deep look to this API that can do much more!

+ Share
Disclosure

Remi

Remi is an expert WordPress developer that coded many great free and premium themes and plugins. His experience on WordPress allows him to produce great stuff and to propose advanced tutorials. Remicorson.com

Related Posts

Back to all articles
  • How to Increase the Memory Limit in WordPress

    How to Increase the Memory Limit in WordPress

  • What is WordPress? A Beginner's Guide!

    What is WordPress? What Can it do & Is it Right for You? A Beginner’s Guide

  • How to Use Git to Push Your Local Site Live

    How to Use Git to Push Your Local Site Live

Coupons

View more deals
  • Recipe Card Blocks Coupon
    15% OFF

    Recipe Card Blocks Coupon

    Running a cooking or food website can be fun (and tasty) – but
    Get This Deal
  • WP 2FA Coupon
    20% OFF

    WP 2FA Coupon

    Security should be at the forefront of all site owner’s min
    Get This Deal
  • Themskingdom Coupon
    20% OFF

    ThemesKingdom Coupon

    First impressions count. As such, you’ll want a WordPress t
    Get This Deal
1 Comment Leave a Reply
  1. Mopra says

    February 1, 2013 at 3:32 pm

    Really cool
    I tried to do something like you but it has increased my Queries (20 to 23 but if I disable my custom query it give me 15 queries)

    Here is my code :

    $exemple = get_transient(‘exemples’);
    if (false === $exemple) {
    $exemple = new WP_Query(‘cat=’.$catss.”);
    set_transient(‘exemples’, $exemple);
    }
    while ($exemple->have_posts()): $exemple->the_post();

    Thanks for your help

    Reply

Leave a Reply Cancel reply

Full Disclosure This post may contain affiliate links, meaning that if you click on one of the links and purchase an item, we may receive a commission (at no additional cost to you). All opinions are our own and we do not accept payments for positive reviews.

THE BEST OF WPKUBE

Some of the best content we have published so far.

BEGINNER GUIDES & REVIEWS

18 Best Cheap WordPress Hosting Providers in 2023 (From $1.99)
210 Best WordPress Hosting Options for 2023 (Pros & Cons)
38 Best Managed WordPress Hosting Providers for 2023 Compared
45 Best WooCommerce Hosting Providers Compared in 2023 (All Budgets)
5Top 9 Landing Page Plugins for WordPress (2023)
69 Best List Building Plugins for WordPress In 2023
7How to Fix the 500 Internal Server Error on Your WordPress Website
8Thrive Themes Review: A Look At The Full Membership
9Beaver Builder Review: Is it The Best Page Builder Plugin for WordPress (2023)?
10OptimizePress Review: Create Landing Pages with Ease
11How to Make a Website: Complete Beginner’s Guide
12Top 22 Best Free Stock Photo Resources For Your Site
1317 of the Best Google Fonts for 2023 (And How to Use Them in WordPress)
14How to Start a Blog in 2022 (Step by Step Guide)
15How To Fix ‘503 Service Unavailable’ WordPress Error
1611 Best Contact Form Plugins for WordPress in 2023
17How to Add a Custom Logo to Your WordPress Site
18How to Fix Error Establishing a Database Connection in WordPress

WPX Hosting: 50% OFF

Save 50% on WPX Hosting using our exclusive coupon code.

Get this Deal

Flywheel(our review)

Our Newsletter

Get awesome content delivered straight to your inbox.

Thank you!

You have successfully joined our subscriber list.

.
Featured In Forbes Huffpost Entrepreneur SEJ

About WPKube

WPKube is an online WordPress resource which focuses on WordPress tutorials, How-to’s, guides, plugins, news, and more. We aim to provide the most comprehensive beginner’s guides to anything about WordPress — from installing plugins, themes, automated installs and setups, to creating and setting up pages for your website.

We have over 500+ tutorials, guides, product reviews, tips, and tricks about WordPress. Founded by Devesh Sharma, the main goal of this site is to provide useful information on anything and everything WordPress.

Twitter Facebook

Useful Links

  • Behind the Scenes
  • Beginner Guides
  • WordPress Hosting
  • WooCommerce Themes
  • MeridianThemes
  • Exclusive WordPress Deals
View All Guides »

Reviews

  • WPEngine 33% OFF
  • Thrive Leads
  • Flywheel 33% OFF
  • Divi Theme 20% OFF
  • Thrive Architect
  • Elegant Themes
Reviews »

Deals

  • InMotion Hosting
  • LifterLMS Coupon
  • LiquidWeb Coupon
  • WPEngine Coupon
  • A2 Hosting
  • FloThemes
More Deals »
© Copyright 2023 WPKube ® All Rights Reserved.
  • Contact
  • Site Terms
  • Disclosure
  • Privacy Policy