API Settings (Options)
The options API was added in version 2.7 and allows you to create form fields to save settings. The API automates the saving of options in the database and outputs the necessary HTML code on the screen. This approach allows for the convenient addition of your own options and option sections to existing settings pages (General, Reading, Media, etc.). Alternatively, you can create plugin settings pages without using unnecessary code.
Despite all the conveniences, registration and validation (sanitization) of field values have not gone anywhere - all of this needs to be done manually.
See also: Settings API for Multisite (Network).
Composer packages (wrapper for the settings API):
Note: All POST requests with form data must be sent to the page wp-admin/options.php
. Users must have the manage_options
capability. In multisite versions, they must be Super Admins to submit form data.
All option registration functions: register_setting(), add_settings_*()
etc. must be called on the admin_init hook.
Functions of the Settings API
Registering/Unregistering Options
Adding Sections and Individual Fields
Output to Screen
Errors
Adding Option Fields
A new option field can be added to a section on an existing WordPress options page or on a plugin options page. This is done using the add_settings_field() function.
The callback function must output the HTML code for the field (input, textarea, ...). WordPress will take care of saving the option to the database. Typically, data is saved in the wp_options table, but it can be reconfigured.
The option being added must first be registered with the register_setting() function; otherwise, it will not be saved or updated.
add_settings_field( $id, $title, $callback, $page, $section = 'default', $args = array() );
-
$id - The slug of the option, used as the field identifier. Used in the ID attribute of the tag.
-
$title - The title of the field.
-
$callback - The name of the callback function. The function should fill the field with the necessary
<input>
tag, which will be part of one large form. The name attribute must equal the $option_name parameter from register_setting(). The id attribute usually equals the $id parameter. The result should be immediately output to the screen (echo). -
$page - The menu page where the field will be added. You should specify the slug of the page, i.e., the parameter must equal the $menu_slug parameter from add_theme_page(). For the basic WordPress pages, the names are: general, reading, writing, etc., by analogy...
-
$section - The title of the settings page section to which the field should be added. By default, it is default or may be a section added by the add_settings_section function.
- $args - Additional parameters that should be passed to the callback function.
Adding Option Sections
Option sections are blocks of options highlighted by a title. Instead of creating a new settings page, it is sometimes wiser to add a new section to an existing WordPress settings page - this will make the plugin simpler and WordPress will not be burdened with a new settings page for the plugin.
add_settings_section( $id, $title, $callback, $page );
-
$id - The identifier of the section. Fields are attached to this ID (see add_settings_field()).
-
$title - The title of the section (the name of the block).
-
$callback - The callback function that is executed at the beginning of the section, before the fields are output. In it, you can output text describing the section; it should be output immediately to the screen using echo.
- $page - The type of settings page on which the section will be displayed (general, reading, writing, ...).
Registering Options
register_setting( $option_group, $option_name, $sanitize_callback );
unregister_setting( $option_group, $option_name, $sanitize_callback );
-
$option_group - The name of the group to which the option belongs. It must match the group in the settings_fields() function.
-
$option_name - The name of the option that will be saved.
- $sanitize_callback - The callback function that will process the value before saving.
Outputting Option Sections to the Screen
When the API is used to add options to an existing options page, there is no need to worry about the HTML tag of the form itself, because it is already added to the page, and the options (input fields) will be inserted inside this form. However, when you create a new options page, you need to specify the HTML form tag (form) and its structure.
settings_fields
To output hidden fields and ensure the security of the options form data, use the settings_fields() function: settings_fields( $option_group );
- $option_group - The name of the group. It must match the same parameter in register_setting(). This is the name of the page (its slug) on which the options are displayed.
do_settings_sections
To output the section intended for the options page, you need to use the do_settings_sections() function: do_settings_sections( $page );
- $page - An alternative name (slug) of the page of the section to be displayed. It must match the name of the page in the add_settings_section() function.
The do_settings_fields() function is similar to do_settings_sections(); it also outputs fields for a specific page and section, but these fields are not formatted in a tabular view and are output as they are. Usually, there is no need to call this function directly; you should use do_settings_sections() to output option fields related to the section.
submit_button
The options form needs a submit button. For this, use the submit_button() function.
Outputting the Entire Form Block
Finally, you will need to add the HTML tag