Archive for July 25th, 2012

They suckered me into helping redesign the website for my daughter's school (don't worry—if you're looking at it before July 27, 2012 it's not my design), and they wanted to go with a WordPress site so lots of people could edit it easily. It seemed like a good idea, but it took 4 weeks of tweaking to get it to the point that everyone liked it.

One thing I wanted to do was have a single settings page for all the relevant options (secretary's email, reCaptcha info for forms), etc. While there are tutorials out there, nothing really hung together for me until I started playing with it. To make it simple, I created a single function that would implement all the settings. It uses PHP 5.3, with namespaces and closures (both of which make life much easier by not polluting the global namespace).

The key is to put all the plugin's code under a single unique namespace, and use that namespace as the index for all the options.

Calling it is simple:

namespace MyUniqueName;
$title="Title for the Plugins Page (short enough to fit in the settings menu)";
plugin_settings($title, $defaults);

$defaults is an array of 'key' => $array, where the $array is array('default value', 'descriptive text', 'callback function'), where 'callback function' is the callable used to display the input element for that option. It can be a string representing the name of the function (prepended with the namespace!) or an anonymous function itself. It is passed a single argument, array('key' => $key), where $key is the key used in the original array.

Use the options as $options = get_option(__NAMESPACE__); $my_option = $options[$key];.

A simple callback function for a text box is:

function text_callback($args){
	$name = $args['key'];
	$ns = __NAMESPACE__; // note that __NAMESPACE__ is used as the index for the options in the database
	$options = get_option($ns);
	echo "<input name='{$ns}[$name]' value='{$options[$name]}' />";		
};

And to make it simpler, I created a function generator to do exactly this: textbox($size=40) returns a callback function to output a text box of $size characters.

So a sample $defaults would be:

$defaults = array(
  'email' => array('info@example.org', 'Main Email Address', textbox()),
  'principal' => array('Ed Rooney', 'Principal\'s Full name', textbox(80))
);
Continue reading ‘Simplifying the Settings API for WordPress’ »