Categories
Code WordPress

Storing Data in WordPress Plugins – A Quick Rundown

Coding better WordPress plugins

As I’ve worked with WordPress plugins, I’ve learned new ways of working with WordPress. WordPress has tons of built-in functionality that is very useful and easy to use once discovered.

I am by no means a great PHP coder. I am still learning OOP principles and how to write code better. In creating new WordPress plugins (see a list of my plugins), I have improved how I code: writing more efficient code using WordPress functionality rather than hacks.

One of the methods of coding that I have discovered (thanks to Jeremy Clarke) is using the WP Cache and Transient APIs to store plugin data. It’s made a big difference in the speed of all my plugins.

The following is a quick review of three different ways of storing data when you code plugins or work with WordPress. This is to the best of my knowledge, and I welcome feedback/improvements in the comments.

There are three core ways of storing WordPress plugin data:

1. Options

This is how most plugins store data, and how I always stored data until I discovered the WP Cache API. An option is permanently stored in the database, which was at times inconvenient. At times, I wanted to refresh the cached data every 12 hours. I hacked some code together to do that, not knowing about the other caching functions available (see below).

Combine options for reduced database calls:

When using options, many plugins save each setting as their own option. This makes for multiple database requests for even the most simple functionality. Instead, combine the settings into one option by making all of the plugin settings a single array.

Let’s say we have three settings for an Example plugin: username, password, URL. Instead of having three options (example_username, example_password, and example_url), we can combine them by setting the input name to example[username], example[password] and example[url]. Now, when we get_option('example'), it is an array of the three settings. One get_option() call replaces three.

Functions used:

  • `get_option()`
  • `set_option()`
  • `update_option()`

2. Transients

Transients are my new best friend.

For the recent Lottery Results plugins, it made sense to store lottery numbers results for a few hours, then fetch new data. What I didn’t want to do was to fetch new data every page load. For lottery results, I wanted new data every 6 hours. Enter the WP Transients API. Transients are stored in the options database, but are used, accessed and updated differently.

Once I retrive the lottery data, I store it using Transients:

set_transient('lottery_results_'.$state_code, $results, 60*60*6);

When the lottery widget is displayed, I try to fetch the stored data using:

get_transient('lottery_results_'.$state_code);

If the transient data is not available, that means that it has expired – and only if the data expires do I need to re-generate it. This saves tons of load time, server load, etc. It’s wonderful, and all built in to WordPress. I’ve incorporated the same transient caching in most of my plugins, including the Wunderground weather plugin. Caching the data speeds everything up considerably.

Functions used:

  • `set_transient()`
  • `get_transient()`
  • `delete_transient()`

3. Cache

WP_Object_Cache is WordPress’ class for caching data which may be computationally expensive to regenerate, such as the result of complex database queries. The object cache is defined in wp-includes/cache.php….

The most common use for the object cache is caching the results of expensive SQL queries so they’re not performed multiple times within a page load. In the below example, imagine the $query variable is an expensive SQL query.
WP Cache page on Codex

If your code uses the same database query results over and over, and you perform the query new each time, you are wasting resources. By using the WP Cache functionality, you can make that request once, then store the results. When you need that dataset again, get the results using wp_cache_get(), and you’ll save many hits on the database or calling external files multiple times.

To find out what data is stored in the cache (you will be surprised!), add this to your functions.php file:

add_action('wp_footer', 'show_cache_data_in_footer');
function show_cache_data_in_footer() {
	global $wp_object_cache;
	$wp_object_cache->stats();
}

Functions used:

  • `wp_cache_add()`
  • `wp_cache_set()`
  • `wp_cache_get()`
  • `wp_cache_delete()`
  • `wp_cache_replace()`
  • `wp_cache_flush()`

I hope this helps people who are wanting to code WordPress plugins or are already coding them and want to do it better!

By Zack Katz

Zack Katz is the founder of GravityKit and TrustedLogin. He lives in Leverett, Massachusetts with his wife Juniper.

1 reply on “Storing Data in WordPress Plugins – A Quick Rundown”

Comments are closed.