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

Add Custom Scripts to Your PowerShell Profile

I’ve recently started doing some work with Windows PowerShell. One problem I wanted to solve was placing my scripts in source control and having them in a non-standard folder. Below is a bit of code you can put in your Microsoft.PowerShell_profile.ps1 file to look for all .ps1 files in a given directory. Just be sure to change the folder in the code below to your PowerShell code folder and all will be good.

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.

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.

WordPress: Get All Users Having a Specific Role

I was searching online for a quick solution to retrieving a list of users from WordPress by role. I found a solution but decided to check the core user functions for a better alternative. As of WordPress 3.1, you can use the handy get_users() function.

Old Way
You had to create your own function that used the WP_User_Search() object.

function getUsersWithRole($role) {
    $wp_user_search = new WP_User_Search($usersearch, $userspage, $role);
    return $wp_user_search->get_results();
}
$editors = getUsersWithRole('editor');

New Way

$editors = get_users('role=editor');

WordPress 3.0 and Menu Dropdown Box

I’ve started to play around with WordPress 3.0 Beta 2 and the new menu system it has. While playing around with it, I thought it might be nice to include a dropdown box of menu choices on a settings page for a theme. So I made a function similar to that of the dropdown box for categories (wp_dropdown_categories). I called this one wp_dropdown_nav_menus.

function wp_dropdown_nav_menus($args = ''){
	$defaults = array(
		'echo' => 1,
		'id' => '',
		'orderby' => 'name',
		'order' => 'ASC',
		'hide_empty' => 1,
		'selected' => 0,
		'class' => 'postform',
		'tab_index' => 0,
		'show_option_all' => '',
		'show_option_none' => '',
	);

	$r = wp_parse_args( $args, $defaults );
	extract( $r );

	$tab_index_attribute = '';
	if ( (int) $tab_index > 0 )
		$tab_index_attribute = " tabindex="$tab_index"";

	$terms = get_terms( 'nav_menu', array( 'hide_empty' => $r['hide_if_empty'], 'orderby' => $r['orderby'], 'order' => $r['order'] ));

	if(! empty($terms)){
		$output = "";
	}

	if ($echo){
		echo $output;
	}

	return $output;
}

If you want to use this in your code, please change the prefix of the function from wp_ to something more useful for your purpose. Just in case WordPress comes out with a function similar to this one.

Windows Mobile Event Viewer – First Release

Alright troops…who’s gonna be first to test out the latest and greatest Event Viewer for Windows Mobile?

I’m not going to say its perfect, because the only person to test it has been me! If you don’t like it, then just don’t tell your friends about it. If you do like it, by all means get your developer friends to use it in their Windows Mobile applications.

You can download the files here.