And which one should I use?
What’s the difference between Options API and Setings API?
I have a theme with over 100 admin options, how should I register & store them? At this moment I’m using Options API but I’m not too happy with it, every single option is being registered separately so they create a lot of database queries.
I see three ways of storing WordPress options/settings at the moment:
1. Options API – the old (the worst?) way.
(I’m using this one already)
<form method="post" action="options.php">
<?php wp_nonce_field('update-options'); ?>
// all the inputs
<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="option1,option2,option3,..." />
<p class="submit">
<input type="submit" value="<?php _e('Save Changes') ?>" />
</p>
</form>
And then in the front end:
echo get_option('option1');
Looks ugly and I don’t think that’s the right way of storing big number of options (I’d love to group them somehow).
2. Options API – 1 option as a big array.
I’m not sure here how to update this array using html forms, anyone?
I believe it should go like:
$opt['option1'] = foo
$opt['option2'] = foo
$opt['option3'] = bar
update_option('theme_settings',$opt);
And then in front end:
$settings = get_option('theme_settings');
echo $settings['option1'];
echo $settings['option2'];
echo $settings['option3'];
I’ve heard it really decreases number of queries and I have them all as one option. I’d love to use this one but as I mentioned above I’m not sure how to update this array of options within WordPress admin form. I guess: <input type="hidden" name="theme_settings" value="option1,option2,option3,..." />
will overwrite the array instead of adding new elements?
3. Settings API – the new way.
Requires a lot of additional code:
function register_theme_settings() {
//register our settings
register_setting( 'theme-settings', 'option1' );
register_setting( 'theme-settings', 'option2' );
register_setting( 'theme-settings', 'option3' );
register_setting( 'theme-settings', 'option4' );
register_setting( 'theme-settings', 'option5' );
// imagine this for 100 and more options
};
And I’m not sure what’s the difference between this and the method with one option as an array?
tl;dr So, how should I project my theme settings storage so it will be working fast and store all the options/settings in one place so I will be able to, for example, echo/dump them all with one simple function (yes, I want to do that badly but I have no idea how to do that with this Settings API). The second method looks the best so far.
The Options API is primarily a database API, allowing you to get and store values in the options table of the database easily.
The Settings API is an interface API. It allows you to build settings screens in a manner that will adapt with future changes to the WordPress interface, as well as to handle security (nonces and whitelisting and validation and such) automatically and more easily than rolling-your-own-code.
More on Settings API: http://ottopress.com/2009/wordpress-settings-api-tutorial/