WordPress Options Class with JSON Support

A while back I had an article title: WordPress Options Class, which was my way of accessing and storing options in WordPress for some of the plugins that I create. After using WordPress for some time now it’s become apparent that storing options in a serialized array in the database is not the best way to do things. I’ve updated my options class to now save the options going forward as JSON. My hope is that other developers will start to do the same.

Why Do It This Way?

At my previous job, there were several times I had to go into some other plugin’s database tables and update a serialized string because some value on our end changed and there was no way to update it through the UI. Using JSON, makes those sorts of changes much easier.

Notes

Using this will allow your options to still be stored as a serialized array, but the next time you save them using this class, the serialized array will be changed to JSON.

The Code

Object-Oriented Plugins for WordPress

Making your WordPress plugins (and themes for that matter) object-oriented definitely has it’s benefits; most notably not running into naming conflicts with other functions. You are essentially creating a new workspace for your functions to live and could have the same function name as found in the WordPress core.

How to Get Started?

Plugin Declaration

The first part of the code is your plugin declaration in the commented out lines. This is required by WordPress!

Class Declaration

You will have to create a unique class name for your plugin. Once you’ve figured out what to call it, you’ll want to add a __construct() function to it. This is typically where I will put all of my actions and filters, along with any special initialization code for my plugin.

Actions and Filters

This line may look a little different from normal add_action() and add_filter() calls. For our second parameter, instead of passing a string for our function name, we are passing an array containing our class object, $this, and the corresponding function in our class, admin_init. The function name is arbitrary; it could be erics_super_awesome_admin_function for all WordPress cares. I typically name my function the same thing as the action or filter for clarity on my end. It seems easier to find things that way to me, and I know precisely when it’s going to happen.

Instance of Class

This is a major step. If you forget this, your plugin (or theme) won’t work. We have to create an instance of our class for the code to run. I’ve created a new variable named $my_object_oriented_plugin that contains an instance of the class.

Questions?

Fire away in the comments.

I would highly recommend that anyone doing WordPress plugin or theme development look into writing object-oriented code. It’s not as hard or scary as people think it is.

Custom Taxonomy Control for the Theme Customizer

Otto had a series of articles highlighting how to use the theme customizer in your WordPress themes instead of creating an options panel.

One of his articles covered making your own custom control and it inspired me to make a custom control for showing a certain taxonomy dropdown on the theme customizer. At first, I had just wanted this for the purpose of choosing from a dropdown of categories to use as featured posts in my theme. Then I thought it could really just as well be used for any taxonomy object. Behind the scenes, this control is using the wp_dropdown_categories function, which allows you to build a dropdown of categories (or any other registered taxonomy).

The code for the control really wasn’t bad. After actually implementing this and trying to use it, I found that my changes were not being saved. There is a data-customize-setting-link attribute that must be appended to your element. Below is the code from my functions.php file and the class being called by functions.php:

WordPress Options Class

I came up with an object-oriented way of working with my plugin’s options for my work projects.

Creation

So then all I have to do in my class is create an instance of this:
$options = new My_Plugin_Options('my_plugin_options_name');

Accessing

It will store all of your plugin options in 1 record in the database as an array. You can easily access one of the options by doing this:
$this->options->my_first_option

Saving

After updating all the options, we need to save them at once.
$this->options->save();

My Setup

I’ve created a folder inside wp-content/plugins for some code that I use on multiple plugins/themes at work. I don’t have the plugin declaration at the top of the main file, so it doesn’t get recognized as something needing activation. All I do is require the main file in my plugins and themes and that file includes the rest of my classes; the Options class being one of them. I’ve also created a couple classes for dealing with custom post types and taxonomies.