Skip to content
scribu edited this page Jun 19, 2011 · 1 revision

Every plugin that's more than 100 lines of code has at least one setting stored in the database. The functions that come with WordPress for managing options are ok, but they can be used better.

Here's how the scbOptions class makes your life easier as a plugin developer:

Advantages

All in one place

scbOptions stores all fields in a single database row. Ask any WP plugin author worth his salt and he will agree that that's the way to go.

Clean up on your way out

When the plugin is uninstalled, the options will also be deleted to keep the database clean.

Usage

Creating a set of options

$defaults = array(
	'color' => 'orange',
	'size' => 'large',
	'version' => '1.5'
);
$options = new scbOptions('pumpkin_settings', __FILE__, $defaults);
The first argument is the option name.

The second arg. is a reference to the main plugin file.

The third argument is an array with default values (optional).

Accessing fields

// Get a single setting
$options->color;
// Result: 'orange'
// Get a single setting (traditional)
$options->get('color');
// Result: 'orange'
// Get a selection of fields
$options->get(array('color', 'size'));
// Result: array([color] => orange, [size] => large)
// Get all fields
$options->get();
// Result: Array([color] => orange, [size] => large, [version] => 1.5)

Modifying fields

// Modify a single setting
$options->color = 'blue';
// Modify a single setting (traditional)
$options->set('color', 'blue');
// Modify one or more fields
$options->set(array(
'color' => 'blue',
'size' => 'medium'
));
// Reset all fields to default values
$options->reset();
Clone this wiki locally