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

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:

Edit Post Author Name in WordPress Feeds

Just today someone approached me at work about the author’s name displaying in Google search results instead of our company name. You can change what shows for each individual author in the your profile for WordPress, but if you want to keep it all the same regardless of who posted the article (useful for a business), you can use the following code as an example:

What’s Going On in this Code?

The first thing we are doing is using an action in the feed file to set a global variable that says we are currently in the feed.

Note: WordPress offers different types of feeds, so you may want to perform this for each of them. If you look in your wp-includes/ folder, you should see some files that begin with “feeds-“. Each of these has their own hook you can use to create the global variable (as done above in the my_rss2_ns function). I’ve listed the file and the hooks below:

File Hook
wp-includes/feeds-rdp.php rdf_ns
wp-includes/feeds-rss.php rss_ns
wp-includes/feeds-rss2.php rss2_ns

The last thing we are doing is simply checking if our global variable is true, and if so, updating the author text with something we’ve specified.

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:

Allowing Editors to Clear the Cache using Quick Cache

I started using Quick Cache on a site recently and some of the publishers (setup as editors) wanted to be able to clear the cache manually to ensure their posts went live right away. So I wrote a quick script to handle that. I’ll probably release a plugin for this eventually, but for now…here’s the code:

function check_publish(){
    global $current_user, $post;
        
    $capability = 'publish_';
    // Check current post type...if we have one available
    if($post->post_type != ''){
        // Set the capability we are looking for to publish_s
        $capability .= $post->post_type . 's';
    } else {
        // Set the default capability to publish_posts
        $capability .= 'posts';
    }
    
    // Check if the user has rights to publish a post
    return current_user_can($capability);
}
add_filter('ws_plugin__qcache_ms_user_can_see_admin_header_controls', 'check_publish');

This will include the Clear Cache button in the admin section of WordPress.

Update: I’ve released this plugin on wordpress.org.