Using WordPress ‘add_settings_section()’ PHP function

The add_settings_section() WordPress PHP function is part of the Settings API. It is used to define new settings sections for an admin page. To show settings sections in your admin page, use the do_settings_sections() function. To add settings fields to your section, use add_settings_field(). The $callback argument is 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.

Usage

Here’s a basic example of how to use the function:

add_settings_section( 'my_setting_section', 'My Settings Section', 'my_section_callback', 'reading' );

function my_section_callback( $args ) {
    echo 'This is my custom settings section.';
}

In this example, we’re creating a new settings section titled “My Settings Section” on the ‘reading’ settings page.

Parameters

  • $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 the 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. You can create your own using add_options_page().
  • $args (array) Optional: Arguments used to create the settings section.

More information

See WordPress Developer Resources: add_settings_section()

Note: The $page parameter can be interpreted as needing to be the slug name of the admin page created through add_submenu_page() or one of its wrappers (such as add_theme_page() or add_plugins_page()). Instead, it only needs to be a slug-formatted name used by add_settings_field() and do_settings_sections().

Examples

Basic Usage

add_settings_section(
    'my_setting_section', // ID used to identify this section
    'My Settings Section', // Title to be displayed on the administration page
    'my_section_callback', // Callback used to render the description of the section
    'reading'  // Page on which to add this section
);

function my_section_callback( $args ) {
    echo 'This is my custom settings section.';
}

This code will create a new settings section titled “My Settings Section” on the ‘reading’ settings page. When the section is rendered, it will call my_section_callback(), which will echo out “This is my custom settings section.”

Using a Class

class CustomSetting {
    function __construct() {
        add_action('admin_init', array($this,'wp_setting_init'));
    }
    function wp_setting_init() {
        register_setting('reading', 'page_limit');
        add_settings_section(
            'wp_custom_setting_section',
            'WP Custom Setting Section',
            array($this,'wp_custom_setting_section_cb'),
            'reading'
        );
    }
    function wp_custom_setting_section_cb() {
        esc_html_e('Page limit is 10','text-domain');
    }
}
new CustomSetting();

This example shows how to use add_settings_section() within a class. The CustomSetting class creates a new settings section when instantiated, with a callback that echoes “Page limit is 10”.