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.


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

New Plugin: Speak Pirate

Argh mateys! Shiver me timbers! Sometimes it feels good to step out of your normal coding routine and do something a little different.

Yesterday, @arod2634 posted this tweet:

One of the APIs listed was a pirate translation api that would take English and turn it into the equivalent as a pirate would say it. What better way to put it to use then make a WordPress plugin out of it: Speak Pirate.

This new plugin be hot off yonder press and working hard to convert ye plain text into pirate jargon. Simply use ye new shortcode [speak_pirate] around ye text you want translated and it shall be done.

[speak_pirate]Text to translate goes here[/speak_pirate]

Enjoy, ye land lubbers!

Hungry For More Information?

Here is the commented 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.


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.

Extending Hide Inactive Sites Plugin

In my Hide Inactive Sites plugin I’ve added several filters to manipulate the data. You can modify the emails sent to the end users and edit the times available in the plugin settings. I thought it might be helpful to show how to change some of these that may want to do so.

Note: please never edit the plugin code directly! You are best off creating your own plugin or putting code in your theme’s functions.php file.

Let’s say that I want to warn my users that their site will be hidden if they haven’t updated it in 2 days. Well, by default, the plugin doesn’t offer you the option to warn in 2 days. It allows you to warn after 1 day of inactivity. One week is the next closest time to warn a user of inactivity. So, let’s add an option for “2 days”:

function add_inactivity_warning_threshold($inactivity_warning_thresholds){
    $inactivity_warning_thresholds[60*60*24*2] = __('2 Days');
    return $inactivity_warning_thresholds;
add_filter('hide-inactive-sites-manage-inactivity-warning-thresholds', 'add_inactivity_warning_threshold', 1, 1);

Ok, how about another example? Let’s edit the text sent in the email when a site is hidden:

function edit_hidden_email_message($message, $site_details){
    $message = '';
    $message .= '

Hey admin, your site is no longer available! You should probably have updated it more often!

'; $message .= ''; } add_filter('hide_inactive_sites_edit_site_hidden_message', 'edit_hidden_email_message', 1, 2);

Other Filters Available

Site Hidden Email

  • Edit list of email recipients: hide_inactive_sites_edit_site_hidden_to_emails
  • Edit email headers: hide_inactive_sites_edit_site_hidden_headers
  • Edit subject line: hide_inactive_sites_edit_site_hidden_subject
  • Edit message (used above): hide_inactive_sites_edit_site_hidden_message

Site Warning Email

  • Edit list of email recipients: hide_inactive_sites_edit_site_almost_hidden_to_emails
  • Edit email headers: hide_inactive_sites_edit_site_almost_hidden_headers
  • Edit subject line: hide_inactive_sites_edit_site_almost_hidden_subject
  • Edit message: hide_inactive_sites_edit_site_almost_hidden_message

Plugin Options

  • Edit update frequencies: hide-inactive-sites-manage-update-frequency
  • Edit inactivity thresholds: hide-inactive-sites-manage-inactivity-thresholds
  • Edit inactivity warning thresholds: hide-inactive-sites-manage-inactivity-warning-thresholds
  • Edit number of minimum posts: hide-inactive-sites-manage-min-posts

WordPress Options Class

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


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');


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:


After updating all the options, we need to save them at once.

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.

New WordPress Plugin: Hide Inactive Sites

At work, we setup a blog server back in 2009. One of our goals was to make sure if people weren’t keeping their blogs up-to-date their site would be hidden from the listing of blogs. Well, I finally took the time to work out an automated solution.

Introducing, Hide Inactive Sites; this baby is a blog server administrator’s dream come true!

It’s setup to be extendable. If you need to edit the query of blogs being returned, fine, change it using a filter. Need more time options or privacy options, done…add your own using hooks.

It slices, it dices, and it even hides your old, out-dated blogs.