Function: register_setting()

The register_setting() function associates an option group (the $option_group argument passed to settings_fields() and do_settings_sections(), above) with a database entry and a data-validation callback function.

$option_group: the name of the option group. Must be the same as the $option_group defined in settings_fields(). This is the "flag" that ties everything together outside of the database (i.e. within the Settings page)

$option_name: the name of the option as stored in the database.

$sanitize_callback: the name of the function that holds the data validation and whitelisting

Since we have only one database entry, which is an array that holds all of our options, we only need to call register_setting() once. If we instead had multiple database entries, we would have to call register_setting() once for each database entry.

Note: this function call needs to be hooked into admin_init.

With this simple step out of the way, we now move on to the meat of our Settings API implementation: building out the form sections and form fields.

Define Form Sections and Fields

Separating Settings Per Tab

Our Settings page has multiple tabs, but we only defined one form. We only have one form, and therefore call settings_fields() and do_settings_sections() only once within that form. If you recall, both of these settings have arguments that tie both the database entry (via register_setting()) and the settings sections (via add_settings_section()) to the form.

We want to ensure that the form only outputs/handles the settings applicable to each tab. There are at least two ways to accomplish this separation:

Assign each tab's related options to a separate $option_group and $page, and add a current-tab conditional around the settings_fields() and do_settings_sections() calls.

Assign all options, regardless of tab, to the same $option_group and $page, and add a current-tab conditional around the functions that register the form sections and form fields.

Because it is what I thought of first, I went with the latter option; however, the former option might make a bit more semantic sense (assuming it works; I've not tried it).

Essentially, a separate file is included, depending on which tab is displayed. Each file contains the code that registers the form sections and form fields related to each tab.

The $pagenow global is basically the URL of the current page. When we hooked our Settings page into the admin menu, the add_theme_page() function appends a parameter "page" to the URL, in the form of "page=oenology-options". Our custom function that handles tabs also adds a parameter, "tab", to the URL, in the form of "tab=general". So, if we are on the General tab of our Settings page, the URL would look something like:

Defining Form Sections

Function: add_settings_section()

As its name implies, the add_settings_section() function defines a form section. Sections are simply groupings of (presumably similar/related) form fields. A settings form may have any number of sections. All form fields must be added to form sections.

$section: the name/id of the section. This argument correlates to the $section argument that is passed to each call to add_settings_field(), and is also passed as the CSS ID selector of the section's containing DIV.

$title: the title of the section, as output on the form.

$callback: function that contains markup/text to be output with the section, before associated settings fields are output (e.g. to provide explanatory text/description)

$page: the name of the Settings page for which to output the section. Must be the same as the $page argument passed to do_settings_sections(), which we addressed previously

We simply add one call to add_settings_section() for each form section.

General Tab

For the General tab, we will define two sections: one for Header options and one for Footer options:

This is an exceptionally detailed tutorial, Chip! Looks like you beat me to it – I had started writing one based on my experience.

I have grappled with a lot of these concepts while rewriting the options for Suffusion. In my case the situation was more complex – I have 2 levels of tabs: a horizontal one similar to your General / Varietals, and a vertical one within each. So basically I have GUI settings and Back-End Settings, with the first having Header settings, footer settings, fonts etc, and the back-end having tabs for SEO, Analytics etc.

Also, instead of using one form, I had to use one form for each tab. Otherwise with my number of options I was running into issues with PHP installations that had Suhosin enabled (that restricts the number of post variables). That pushed the complexity to a whole different label, and I had to keep track of post variables in other forms (otherwise the whole array gets rewritten to only have options from the current form). Anyway, I guess you can see what I am saying when I submit my next version for review.

One small note – in page 3 you are retrieving “settings-updated”. That applies to WP 3.1 onwards. In WP 3.0 you get “updated” as the returned URL parameter.

Great (and comprehensive!) tutorial here! One thing I had to find out the hard way: if you use the Settings API, you need the ‘manage_options’ capability to update the options. Normally this works fine, as only administrators have those two capabilities, but if you give editors the ability to edit theme options, they’ll need ‘manage_options’ too. This is built into the settings api, since it’s assumed it’s going to be used for options pages.

Are you 100% certain that you are unable to use the edit_theme_options capability with add_theme_page()? I can confirm that I have used it with no problems whatsoever, and edit_theme_options is the capability officially recommended by the WordPress Theme Review Team and far-more-expert-than-I Theme developers such as Justin Tadlock.

Looks like a fantastic tutorial.
I didn’t read it yet, since now is not a good time for me to do this and so I wanted to print out the whole tutorial which sadly didn’t work.
Not even using the print button to print individual pages worked but cropped content on each article.
So I have to come back another time or cut and paste each part individually.
Nevertheless, I think it will be worth it an am very greatful for this tutorial.

Check the post pagination links at the bottom of the post. I added an “all” link, that will output the entire post on a single page – specifically so that this post could be printed. Let me know if it’s not working for you!

I’ve read your article, and follow most of it. But I have a problem with tabs. As you said, you haven’t tried 2 options for tabs that handle form fields which are in current tabs. Sadly, when I update the form, only fields in current tab are save, fields in other tabs are ignored.

I’m thinking that WP don’t automatically recognize tabs and save them. We need to do it ourselves. Do you know how to make it easily?

The method that I describe here separates, via PHP, the form fields on one tab from the form fields on other tabs. When viewing one tab, the form fields on other tabs are never called. They don’t currently exist on the page. So, it is true: WordPress doesn’t save any settings not on the current page, because at the current point in time, WordPress doesn’t load the fields that allow those other settings to be modified.

To do something different, such as loading all the settings on a single page, and then separate them via tabs, you’d have to take an entirely different approach (such as using jQuery to show/hide groups of settings).

@Chip, @Rilwis,
How I handled this is by invoking the hidden fields in the validation function itself. Basically if you have separate forms you tend to lose the settings of other forms unless you have them conveyed to the back-end. Since you are anyway constructing the forms based on information you have in the back-end, it is easy to pull up the options from other screens in the validation function without passing them at form submission.

Moreover I prefer passing it through the validation function because that keeps your form lighter.

how can i display a message based on which button has been clicked for example if i click reset i would like it to display a message saying “settings reset” also like Sayontan says i had to change “settings-updated” to “updated” within the form code as it didn’t show the message “settings saved” when i clicked save settings

thanks for this tutorial i’m building an options page using this method and trying to move all my old options over to this but i cannot get multi-select checkboxes to save as i have some older code to grab all pages into an array created in wordpress like

(Sorry for the delayed response)
‘edit_theme_options’ for the theme page is fine. It’s the Settings API that requires the ‘manage_options’ capability. The settings API requires you to send form data to /wp-admin/options.php, which, on lines 30-31 has this code:

The theme options page that this code creates will work without a problem, and saving the content will work without a problem — most of the time.

The case where it doesn’t work is when you have anybody who can edit theme options but cannot manage options.

For example, I program clients’ sites with a non-standard ‘Admin Lite’ role which is an editor that can create, edit, and delete non-Administrator users, as well as manage theme options. Because of the way options.php handles capabilities, I cannot use the settings API to validate or save the data from my theme options page. These are very specific use cases, and it might help to know about this behavior for anybody looking up this tutorial in the future. Just trying to share some hard-won knowledge in case it’s useful.