Codex tools: Log in
Languages: English • 日本語 • Português do Brasil • 中文(简体) • (Add your language)
Contents |
The Settings API, added in WordPress 2.7, allows admin pages containing settings forms to be managed semi-automatically. It lets you define settings pages, sections within those pages and fields within the sections.
New settings pages can be registered along with sections and fields inside them. Existing settings pages can also be added to by registering new settings sections or fields inside of them.
Organizing registration and validation of fields still requires some effort from developers using the Settings API, but avoids a lot of complex debugging of underlying options management.
NOTE: When using the Settings API, the form posts to wp-admin/options.php which provides fairly strict capabilities checking. Users will need 'manage_options' capability (and in MultiSite will have to be a Super Admin) to submit the form.
The functions are found in wp-admin/includes/plugin.php and wp-admin/includes/template.php
|
|
|
|
You can add new settings fields (basically, an option in the 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())
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 fewer 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)
register_setting( $option_group, $option_name, $sanitize_callback )
unregister_setting( $option_group, $option_name, $sanitize_callback )
To display the hidden fields and handle security of your options form, the Settings API provides the settings_fields() function.
settings_fields($option_group)
For a complete example using a PHP class,see the WordPress code for the Custom Background screen: wp-admin/custom-background.php
<?php
// ------------------------------------------------------------------
// 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>
register_setting('reading','eg_setting_name');
}// eg_settings_api_init()
add_action('admin_init', '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() {
echo '<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() {
echo '<input name="eg_setting_name" id="gv_thumbnails_insert_into_excerpt" type="checkbox" value="1" class="code" ' . checked( 1, get_option('eg_setting_name'), false ) . ' /> Explanation text';
}
?>