15

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.

1 Answer 1

12

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/

5
  • 3
    +1. The Settings API is the correct approach, and will handle your 100 Theme options without batting an eye. Some fool wrote a really long-winded tutorial for incorporating the Settings API in WordPress Themes. Feb 2, 2012 at 3:23
  • Otto, @Chip Bennett thank you guys. I have one more additional question - according to documentation I should create multiple settings groups for each admin page, will it affect performance / number of queries somehow? Is there anything wrong with keeping all the options within one group (let's say 'theme-settings') as I explained in my too long to read question? :) Feb 2, 2012 at 12:34
  • 2
    I would recommend using only one options group. If you need UI organization, use settings sections, and or settings page tabs. Feb 2, 2012 at 14:30
  • @Chip Bennett, alright, thanks a lot :) Great tutorial btw. Feb 2, 2012 at 15:43
  • @ChipBennett Link: incorporating the Settings API in WordPress Themes in your comment is broken. Aug 18, 2018 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.