-
Notifications
You must be signed in to change notification settings - Fork 55
scbOptions
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:
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. When the plugin is uninstalled, the options will also be deleted to keep the database clean.$defaults = array(
'color' => 'orange',
'size' => 'large',
'version' => '1.5'
);
$options = new scbOptions('pumpkin_settings', __FILE__, $defaults);
The second arg. is a reference to the main plugin file.
The third argument is an array with default values (optional).
// 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)
// 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();