add_settings_section()

最后更新于:2021-11-25 19:40:56

add_settings_section( string$id, string$title, callable$callback, string$page)

Add a new section to a settings page.

参数

$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();

源文件

文件: gc-admin/includes/template.php

function add_settings_section( $id, $title, $callback, $page ) {
	global $gc_settings_sections;

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

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

	$gc_settings_sections[ $page ][ $id ] = array(
		'id'       => $id,
		'title'    => $title,
		'callback' => $callback,
	);
}

add_settings_section(
	'eg_setting_section',
	__( 'Example settings section in reading', 'textdomain' ),
	'gcdocs_setting_section_callback_function',
	'reading'
);

/**
 * Settings section display callback.
 *
 * @param array $args Display arguments.
 */ 
function gcdocs_setting_section_callback_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>callback: ' . esc_html( $args['callback'] ) . '</p>';             // callback: eg_setting_section_callback_function
}
class custom_setting {

	function __construct() {
		/**
		 * register gc_setting_init to the admin_init action hook
		 */
		add_action('admin_init', array($this,'gc_setting_init'));		
	}

	function gc_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(
	        'gc_custom_setting_section',
	        'GC Custom Setting Section',
	        array($this,'gc_custom_setting_section_cb'),
	        'reading'
	    );
	}
	 
	/**
	 * callback functions
	 */
	 
	// section content cb
	function gc_custom_setting_section_cb() {
	    esc_html_e('Page limit is 10','text-domain');
	}
 
}

new custom_setting();