add_settings_section()
Add a new section to a settings page.
Part of the Settings API. Use this to define new settings sections for an admin page. Show settings sections in your admin page callback function with do_settings_sections(). Add settings fields to your section with add_settings_field().
The $callback argument should be the name of a function that echoes out any content you want to show at the top of the settings section before the actual fields. It can output nothing if you want.
No Hooks.
Return
null
. Nothing (null).
Usage
add_settings_section( $id, $title, $callback, $page, $args );
- $id(string) (required)
- Slug-name to identify the section. Used in the 'id' attribute of tags.
- $title(string) (required)
- Formatted title of the section. Shown as the heading for the section.
- $callback(callable) (required)
- Function that echos out any content at the top of the section (between heading and fields).
- $page(string) (required)
- The slug-name of the settings page on which to show the section. Built-in pages include 'general', 'reading', 'writing', 'discussion', 'media', etc. Create your own using add_options_page();
- $args(array)
Arguments used to create the settings section.
Default: array()
-
before_section(string)
HTML content to prepend to the section's HTML output. Receives the section's class name as %s.
Default: '' -
after_section(string)
HTML content to append to the section's HTML output.
Default: '' - section_class(string)
The class name to use for the section.
Default: ''
-
Examples
#1 Demo [auto-translate]
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. |