Consolidate Options with Arrays in your WordPress Themes

As more and more themes start growing in complexity and features, storing information in the database is a natural progression towards saving any user preferences for that particular theme. In this article we’re going to talk about how you can consolidate all those theme options into a single database entry. Then we’re going to create a set of functions to easily interface with those options for use throughout your template files.

In this example, we’re going to look at the popular k2 theme and see what it takes to consolidate their settings into one single database entry.

Upon installation, k2 runs an install function that adds several settings into the database.

function install() {
	add_option( 'k2version', K2_CURRENT );
	add_option( 'k2asidescategory', '0' );
	add_option( 'k2livesearch', '1' );
	add_option( 'k2rollingarchives', '1' );
	add_option( 'k2archives', '0' );
	add_option( 'k2sidebarmanager', '0' );
	add_option( 'k2columns', '2' );

	// Added 1.0-RC8
	add_option( 'k2animations', '1' );
	add_option( 'k2entrymeta1', __('Published on %date% in %categories%. %comments% %tags%', 'k2_domain') );
	add_option( 'k2entrymeta2', '' );
	add_option( 'k2widgetoptions', array() );

	$defaultjs = "// Lightbox v2.03.3 - Adds new images to lightbox\nif (typeof myLightbox != 'undefined' && myLightbox instanceof Lightbox && myLightbox.updateImageList) {\n\tmyLightbox.updateImageList();\n}\n";
	add_option( 'k2ajaxdonejs', $defaultjs );
}

We’re going to recreate that install function, but first, lets add a new function that stores the default values for each option as an array:

function k2_default_options() {
	$defaultjs = "// Lightbox v2.03.3 - Adds new images to lightbox\nif (typeof myLightbox != 'undefined' && myLightbox instanceof Lightbox && myLightbox.updateImageList) {\n\tmyLightbox.updateImageList();\n}\n";
	$options = array(
		'version' => K2_CURRENT,
		'asidescategory' => false,
		'livesearch' => true,
		'rollingarchives' => true,
		'archives' => false,
		'sidebarmanager' => false,
		'archives' => false,
		'columns' => '2',
		'animations' => true,
		'entrymeta1' => __('Published on %date% in %categories%. %comments% %tags%', 'k2_domain'),
		'entrymeta2' => '',
		'widgetoptions' => array(),
		'ajaxdonejs' => $defaultjs,
	);
	return $options;
}

You’ll noticed that I removed all prefixes of k2 as their not needed when everything is being stored under one entry. I also converted all the 0’s and 1’s into their boolean values. Now let’s recreate the install function:

function install() {
	add_option( 'kaytwo', k2_default_options() );
}

It’s as simple as that. Retrieving those values is just as easy:

$kaytwo = get_option( 'kaytwo' );

echo $kaytwo['version'];
echo $kaytwo['columns'];
echo $kaytwo['entrymeta1'];

Updating options involves retrieving the entire options array and updating only those that need to be updated.

$kaytwo = get_option( 'kaytwo' );

$kaytwo['version'] = '1.0';
$kaytwo['columns'] = '4';
$kaytwo['entrymeta2'] = __('Posted in %categories%. Tagged: %tags%', 'k2_domain');

update_option( 'kaytwo', $kaytwo );

Remember to include all options, even those that are not being updated themselves. Failure to do so removes those options from the database completely.

Deleting an option is similar to updating. Retrieve the entire options array and unset those that need to be removed:

$kaytwo = get_option( 'kaytwo' );

unset( $kaytwo['sidebarmanager'] );
unset( $kaytwo['asidescategory'] );

update_option( 'kaytwo', $kaytwo );

And that’s how you would consolidate all your options under a single database entry. But why stop here? Since we’re going to be interfacing with the database all throughout our WordPress theme, let’s make these calls into functions.

define( 'THEMEOPTIONS', 'kaytwo' );

/**
 * Returns the value of an option from the db if it exists.
 *
 * @since 0.4
 *
 * @param string $name Option Name.
 * @return mixed Returns the option's value if it exists, false if it doesn't.
 */
function framework_get_option( $name ) {
	$options = get_option( THEMEOPTIONS );
	if ( isset($options[$name]) ) {
		return $options[$name];
	} else {
		return false;
	}
}

/**
 * Adds an option to the options db.
 *
 * @since 0.4
 *
 * @param string $name Option Name. Must be unique.
 * @param mixed $value Option Value.
 * @return bool True on success, false if the option already exists.
 */
function framework_add_option( $name, $value ) {
	$options = get_option( THEMEOPTIONS );
	if ( $options and !isset($options[$name]) ) {
		$options[$name] = $value;
		return update_option( THEMEOPTIONS, $options );
	} else {
		return false;
	}
}

/**
 * Updates an option to the options db.
 *
 * @since 0.4
 *
 * @param string $name Option Name. Must be unique.
 * @param mixed $value Option Value.
 * @return bool true|false
 */
function framework_update_option( $name, $value ) {
	$options = get_option( THEMEOPTIONS );
	if ( $value != $options[$name] ) {
		$options[$name] = $value;
		return update_option( THEMEOPTIONS, $options );
	} else {
		return false;
	}
}

/**
 * Deletes an option from the options db.
 *
 * @since 0.4
 *
 * @param string $name Option Name. Must be unique.
 * @return bool true|false
 */
function framework_delete_option( $name ) {
	$options = get_option( THEMEOPTIONS );
	if ( $options[$name] ) {
		unset( $options[$name] );
		return update_option( THEMEOPTIONS, $options );
	} else {
		return false;
	}
}

Just define THEMEOPTIONS as the name of the database entry and your all set.

To read more on this topic, check out this post on Striderweb.

2 Tweets

Additional comments powered by BackType

About Ptah

I’m a 21 year old web developer, entrepreneur, and founder of Design by Craftsmen, web creative studio specializing in custom WordPress solutions. Get to know me.

Twitter