Ready to get started?Download WordPress


Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Difference between revisions of "Settings API"

m (Removed "New page created" category)
m (add external reference)
Line 120: Line 120:
} // eg_setting_callback_function()
} // eg_setting_callback_function()
?> %%%
?> %%%
== External References ==
[http://ottodestruct.com/blog/2009/wordpress-settings-api-tutorial/ WordPress Settings API Tutorial] by Otto
[[Category:Advanced Topics]]
[[Category:Advanced Topics]]

Revision as of 14:12, 4 November 2009


WordPress 2.7 adds a Settings API for inserting settings and setting sections into the existing WordPress settings panels. This means directly in those pages and not as a new page.

The functions are found in wp-admin/includes/plugin.php and wp-admin/includes/template.php

Function Reference

Setting Register/Unregister
Add Field/Section

Adding Setting Fields

You can add new settings fields (basically, an option for wp_options database table but totally managed for you) to the existing WordPress pages using this function. Your callback function just needs to output the appropriate html input and fill it with the old value, the saving will be done behind the scenes. You can create your own sections on existing pages using add_settings_section() as described below.

NOTE: You MUST register any options you use with add_settings_field() or they won't be saved and updated automatically. See below for details and an example.

add_settings_field($id, $title, $callback, $page, $section = 'default', $args = array())
  • $id - String for use in the 'id' attribute of tags.
  • $title - Title of the field.
  • $callback - Function that fills the field with the desired inputs as part of the larger form. Name and id of the input should match the $id given to this function. The function should echo its output.
  • $page - The type of settings page on which to show the field (general, reading, writing, ...).
  • $section - The section of the settings page in which to show the box (default or a section you added with add_settings_section, look at the page in the source to see what the existing ones are.)
  • $args - Additional arguments

Adding Settings Sections

Settings Sections are the groups of settings you see on WordPress settings pages with a shared heading. In your plugin you can add new sections to existing settings pages rather than creating a whole new page. This makes your plugin simpler to maintain and creates less new pages for users to learn. You just tell them to change your setting on the relevant existing page.

add_settings_section($id, $title, $callback, $page)
  • $id - String for use in the 'id' attribute of tags.
  • $title - Title of the section.
  • $callback - Function that fills the section with the desired content. The function should echo its output.
  • $page - The type of settings page on which to show the section (general, reading, writing, media etc.)

Registering Settings

register_setting( $option_group, $option_name, $sanitize_callback )
unregister_setting( $option_group, $option_name, $sanitize_callback )


Adding a settings section with a new field in it

// ------------------------------------------------------------------
 // Add all your sections, fields and settings during admin_init
 // ------------------------------------------------------------------
function eg_settings_api_init() {
// Add the section to reading settings so we can add our fields to it
add_settings_section('eg_setting_section''Example settings section in reading''eg_setting_section_callback_function''reading');
// Add the field with the names and function to use for our new settings, put it in our new section
add_settings_field('eg_setting_name''Example setting Name''eg_setting_callback_function''reading''eg_setting_section');
// Register our setting so that $_POST handling is done for us and our callback function just has to echo the <input>
// eg_settings_api_init()
// ------------------------------------------------------------------
 // Settings section callback function
 // ------------------------------------------------------------------
 // This function is needed if we added a new section. This function 
 // will be run at the start of our section
function eg_setting_section_callback_function() {
'<p>Intro text for our settings section</p>';
// ------------------------------------------------------------------
 // Callback function for our example setting
 // ------------------------------------------------------------------
 // creates a checkbox true/false option. Other types are surely possible
function eg_setting_callback_function() {
$checked = \"\";
     // Mark our checkbox as checked if the setting is already true
     if (get_option('eg_setting_name')) 
$checked = \" checked='checked' \";
     echo \"<input 
{$checked} name='eg_setting_name' id='gv_thumbnails_insert_into_excerpt' type='checkbox'
 value='eg_setting_name' class='code' /> Explanation text\";
 } // eg_setting_callback_function()

External References

WordPress Settings API Tutorial by Otto