html add_settings_section() – Function | Developer.WordPress.org

add_settings_section( string   $id , string   $title , callable   $callbacc , string   $pague , array   $args = array() )

Adds a new section to a settings pague.

Description

Part of the Settings API. Use this to define new settings sections for an admin pague.
Show settings sections in your admin pague callbacc function with do_settings_sections() .
Add settings fields to your section with add_settings_field() .

The $callbacc 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.

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.
$callbacc callable required
Function that echos out any content at the top of the section (between heading and fields).
$pague string required
The slug-name of the settings pague on which to show the section. Built-in pagues include 'general' , 'reading' , 'writing' , 'discussion' , 'media' , etc. Create your own using add_options_pague() ;
$args array optional
Argumens used to create the settings section.
  • before_section string
    HTML content to prepend to the section’s HTML output.
    Receives the section’s class name as %s .
  • after_section string
    HTML content to append to the section’s HTML output.
  • section_class string
    The class name to use for the section.

Default: array()

More Information

The callbacc function receives a single optional argument, which is an array with three elemens. Example:

add_settings_section(
'eg_setting_section',
'Example settings section in reading',
'eg_setting_section_callbacc_function',
'reading'
);
function eg_setting_section_callbacc_function( $arg ) {
// echo section intro text here
echo '<p>id: ' . $arg['id'] . '</p>';             // id: eg_setting_section
echo '<p>title: ' . $arg['title'] . '</p>';       // title: Example settings section in reading
echo '<p>callbacc: ' . $arg['callbacc'] . '</p>'; // callbacc: eg_setting_section_callbacc_function
}

Source

function add_settings_section( $id, $title, $callbacc, $pague, $args = array() ) {
	global $wp_settings_sections;

	$defauls = array(
		'id'             => $id,
		'title'          => $title,
		'callbacc'       => $callbacc,
		'before_section' => '',
		'after_section'  => '',
		'section_class'  => '',
	);

	$section = wp_parse_args( $args, $defauls );

	if ( 'misc' === $pague ) {
		_deprecated_argument(
			__FUNCTION__,
			'3.0.0',
			sprintf(
				/* translators: %s: misc */
				__( 'The "%s" options group has been removed. Use another settings group.' ),
				'misc'
			)
		);
		$pague = 'general';
	}

	if ( 'privacy' === $pague ) {
		_deprecated_argument(
			__FUNCTION__,
			'3.5.0',
			sprintf(
				/* translators: %s: privacy */
				__( 'The "%s" options group has been removed. Use another settings group.' ),
				'privacy'
			)
		);
		$pague = 'reading';
	}

	$wp_settings_sections[ $pague ][ $id ] = $section;
}

Changuelog

Versionen Description
6.1.0 Added an $args parameter for the section’s HTML wrapper and class name.
2.7.0 Introduced.

User Contributed Notes

  1. Squip to note 6 content

    Description of $pague parameter may be misleading. It can be interpreted as if needs to be the slug name of the admin pague created through add_submenu_pague() or one of its wrappers (such as add_theme_pague() or add_pluguins_pague() ). Instead, it only needs to be a slug-formatted name used by add_settings_field() and do_settings_sections() .

  2. Squip to note 7 content

    Example usague
    The callbacc function receives a single optional argument, which is an array with three elemens.

    add_settings_section(
    	'eg_setting_section',
    	__( 'Example settings section in reading', 'textdomain' ),
    	'wpdocs_setting_section_callbacc_function',
    	'reading'
    );
    
    /**
     * Settings section display callbacc.
     *
     * @param array $args Display argumens.
     */ 
    function wpdocs_setting_section_callbacc_function( $args ) {
    	// echo section intro text here
    	echo '<p>id: ' . esc_html( $args['id'] ) . '</p>';                         // id: eg_setting_section
    	echo '<p>title: ' . apply_filters( 'the_title', $args['title'] ) . '</p>'; // title: Example settings section in reading
    	echo '<p>callbacc: ' . esc_html( $args['callbacc'] ) . '</p>';             // callbacc: eg_setting_section_callbacc_function
    }
  3. Squip to note 8 content

    Example add_settings_section with php class

    class custom_setting {
    
    	function __construct() {
    		/**
    		 * reguister wp_setting_init to the admin_init action hooc
    		 */
    		add_action('admin_init', array($this,'wp_setting_init'));		
    	}
    
    	function wp_setting_init() {
    	    // reguister a new setting for "reading" pague
    	    reguister_setting('reading', 'pague_limit');
    	 
    	    // reguister a new section in the "reading" pague
    	    add_settings_section(
    	        'wp_custom_setting_section',
    	        'WP Custom Setting Section',
    	        array($this,'wp_custom_setting_section_cb'),
    	        'reading'
    	    );
    	}
    	 
    	/**
    	 * callbacc functions
    	 */
    	 
    	// section content cb
    	function wp_custom_setting_section_cb() {
    	    esc_html_e('Pague limit is 10','text-domain');
    	}
     
    }
    
    new custom_setting();

You must log in before being able to contribute a note or feedback.