Setting API
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.
There are three main components in the Settings API: The Setting, the Field and the Section.
The Setting is the actual option that is stored inside the WordPress database. The Field is a form control rendered in our options page, such as a text input, or a dropdown select. The Section is just a way to group fields together.
Before we get into writing any code, it’s important to understand the three main components of the WordPress Settings API.
- Fields are individual options that appear on menu pages. Fields correspond to actual elements on the screen. That is, a field is managed by a text box, radio button, checkbox, etc. Fields represent a value stored in the WordPress database.
- Sections are a logical grouping of fields. Whenever you’re working with multiple fields, you’re likely going to be grouping related options together – Sections represent this grouping. Furthermore, if your work includes multiple administration pages, each section often corresponds to its own menu page (though you can also add them to existing sections).
- Settings are registered after you’ve defined both Fields and Sections. Think of Settings as a combination of the Field and the Section to which it belongs.
First, add yourself an options page.
The first step is to create a new item in the admin menu, that will lead to our options page. We use the add_options_page() function during the admin_menu action, like this
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.
There are three main components in the Settings API: The Setting, the Field and the Section.
The Setting is the actual option that is stored inside the WordPress database. The Field is a form control rendered in our options page, such as a text input, or a dropdown select. The Section is just a way to group fields together.
Before we get into writing any code, it’s important to understand the three main components of the WordPress Settings API.
- Fields are individual options that appear on menu pages. Fields correspond to actual elements on the screen. That is, a field is managed by a text box, radio button, checkbox, etc. Fields represent a value stored in the WordPress database.
- Sections are a logical grouping of fields. Whenever you’re working with multiple fields, you’re likely going to be grouping related options together – Sections represent this grouping. Furthermore, if your work includes multiple administration pages, each section often corresponds to its own menu page (though you can also add them to existing sections).
- Settings are registered after you’ve defined both Fields and Sections. Think of Settings as a combination of the Field and the Section to which it belongs.
First, add yourself an options page.
The first step is to create a new item in the admin menu, that will lead to our options page. We use the add_options_page() function during the admin_menu action, like this
function SP_admin_menu() {
add_options_page( 'SP Prolicense', 'SP Prolicense', 'manage_options', 'SP-Prolicense', 'SP_options_page' );
}
add_action( 'admin_menu', 'SP_admin_menu' );
The first argument is the page title, which is displayed in the browser title bar. The second one’s the menu title. The third argument is the capability that’s required to access the options page.The fourth one is called the menu slug. The last argument is the callback function, which will be used to render our settings form.
And the content of the page itself
will be generated by the “SP_options_page” function
<div class="wrap">
<h2>SP Prolicense Plugin Options</h2>
<form action="options.php" method="POST">
<?php settings_fields('SP_plugin_options'); ?>
<?php do_settings_sections('SP-Prolicense'); ?>
<?php submit_button(); ?>
</form>
</div>
settings_fields($option_group) function which renders the hidden input fields and handles the security aspects.
Next, we call do_settings_sections(‘plugin’). This is going to output all of our input fields. Text input boxes, radio fields, anything we like.
register_setting( 'SP_plugin_options', 'SP_plugin_options', 'SP_plugin_options_validate' );
First, we register the settings. We store all our settings in one options field, as an array.The first argument is a group, which needs to be the same as what you used in the settings_fields function call. The second argument is the name of the options. If we were doing more than one, we’d have to call this over and over for each separate setting. The final arguement is a function name that will validate your options.
add_settings_section('SP_plugin_main', 'Main Settings', 'SP_plugin_section_text', 'SP-Prolicense');
This creates a “section” of settings.The first argument is simply a unique id for the section.
The second argument is the title or name (to be output on the page).The third is a function callback.The fourth is a page name. This needs to match the text we gave to the do_settings_sections function call.
That function callback in the third argument should look a bit like
<?php function SP_plugin_section_text() {
echo '<p>Main description of this section here.</p>';
} ?>
You can put any HTML you like here.
add_settings_field('SP_plugin_text_string', 'SP Plugin Text Input', 'SP_plugin_setting_string', 'SP-Prolicense', 'SP_plugin_main');
This function adds the option field to the newly created section, and you will need to place a call to it in your code for every new option you wish to add.
The first argument is simply a unique id for the field.
The second is a title for the field.
The third is a function callback, to display the input box.
The fourth is the page name that this is attached to (same as the do_settings_sections function call).
The fifth is the id of the settings section that this goes into (same as the first argument to add_settings_section).
<?php function SP_plugin_setting_string() {
$options = get_option('SP_plugin_options');
echo "<input id='SP_plugin_text_string' name='SP_plugin_options[text_string]' size='40' type='text' value='{$options['text_string']}' />";
} ?>
Since this is designed with security in mind, we have one last callback to deal with:
function SP_plugin_options_validate($input) {
$options = get_option('SP_plugin_options');
$options['text_string'] = trim($input['text_string']);
if(!preg_match('/^[a-z0-9]{32}$/i', $options['text_string'])) {
$options['text_string'] = '';
}
return $options;
}