add_settings_section()
Creates a new block (section) in which the settings fields are displayed. That is, options are then added to this block using add_settings_field().
You can see such a block on WordPress or plugin settings pages — these are groups of settings divided by a heading.
When creating a plugin, you can simply create a new section on an existing WordPress settings page, instead of creating a separate settings page. This will slightly simplify plugin creation and avoid an extra item in the admin menu. You will point users to the familiar WordPress settings page for plugin configuration.
This is one of a group of functions from the Settings API, it works in conjunction with them.
No Hooks.
Returns
null. Does not return anything.
Usage
add_settings_section( $id, $title, $callback, $page );
- $id(string) (required)
- Identifier of the section, used to "attach" fields to the section. The string that will be used for the id attributes of tags.
- $title(string) (required)
- The section title.
- $callback(callable) (required)
A callback (function) that fills the section with a description. Called between the section title and the section fields, see: do_settings_sections().
If you need to skip this parameter, pass the string
'__return_empty_string'instead.- $page(string) (required)
The page on which to display the section.
Must match the $page parameter in do_setting_sections( $page );
Or it can match the $menu_slug parameter from add_menu_page(), add_theme_page(), add_submenu_page().
Usually $page and $menu_slug are named the same.
Examples of WP page slugs:
general,reading,writing,discussion,media, etc.- $args(array) (WP 6.1)
Arguments used to create the settings section. See do_settings_sections().
Possible array elements:
- before_section(string) - HTML before the section HTML.
- after_section(string) - HTML after the section HTML. Default:
''. - section_class(string) - Class name for the section. If this parameter is specified, then in
before_sectionyou should provide a%splaceholder for sprintf() where the class specified here will appear. Default:''.
By default: []
Examples
#1 Demo
add_settings_section(
'eg_setting_section',
__( 'Example settings section in reading', 'textdomain' ),
'wpdocs_setting_section_callback_function',
'reading'
);
/**
* Settings section display callback.
*
* @param array $args Display arguments.
*/
function wpdocs_setting_section_callback_function( $args ) {
// echo section intro text here
// id: eg_setting_section
echo '<p>id: ' . esc_html( $args['id'] ) . '</p>';
// title: Example settings section in reading
echo '<p>title: ' . apply_filters( 'the_title', $args['title'] ) . '</p>';
// callback: eg_setting_section_callback_function
echo '<p>callback: ' . esc_html( $args['callback'] ) . '</p>';
} #2 Example of use in OOP form (with a php class)
class custom_setting {
function __construct() {
/**
* register wp_setting_init to the admin_init action hook
*/
add_action('admin_init', array($this,'wp_setting_init'));
}
function wp_setting_init() {
// register a new setting for "reading" page
register_setting('reading', 'page_limit');
// register a new section in the "reading" page
add_settings_section(
'wp_custom_setting_section',
'WP Custom Setting Section',
array($this,'wp_custom_setting_section_cb'),
'reading'
);
}
/**
* callback functions
*/
// section content cb
function wp_custom_setting_section_cb() {
esc_html_e('Page limit is 10','text-domain');
}
}
new custom_setting(); #3 See more examples
See the examples on the API settings page.
Notes
- Global. Array. $wp_settings_sections Storage array of all settings sections added to admin pages.
Changelog
| Since 2.7.0 | Introduced. |
| Since 6.1.0 | Added an $args parameter for the section's HTML wrapper and class name. |