add_settings_field()

add_settings_field()函数是Wordpress函数,将新字段添加到“设置”页的某个部分。

add_settings_field( string $id, string $title, callable $callback, string $page, string $section = ‘default’, array $args = array() )

将新字段添加到“设置”页的某个部分。
Add a new field to a section of a settings page.

说明(Description)

设置API的一部分。使用此选项可定义设置字段,该字段将显示为设置页内设置部分的一部分。使用do_settings-sections()中的do_settings_fields()显示字段

$callback参数应该是响应此设置字段的html输入标记的函数的名称。使用get_option()检索要显示的现有值。


返回(Return)

无返回值


源码(Source)

/**
 * Add a new field to a section of a settings page
 *
 * Part of the Settings API. Use this to define a settings field that will show
 * as part of a settings section inside a settings page. The fields are shown using
 * do_settings_fields() in do_settings-sections()
 *
 * The $callback argument should be the name of a function that echoes out the
 * html input tags for this setting field. Use get_option() to retrieve existing
 * values to show.
 *
 * @since 2.7.0
 * @since 4.2.0 The `$class` argument was added.
 *
 * @global $wp_settings_fields Storage array of settings fields and info about their pages/sections
 *
 * @param string $id       Slug-name to identify the field. Used in the 'id' attribute of tags.
 * @param string $title    Formatted title of the field. Shown as the label for the field
 *                         during output.
 * @param string $callback Function that fills the field with the desired form inputs. The
 *                         function should echo its output.
 * @param string $page     The slug-name of the settings page on which to show the section
 *                         (general, reading, writing, ...).
 * @param string $section  Optional. The slug-name of the section of the settings page
 *                         in which to show the box. Default 'default'.
 * @param array  $args {
 *     Optional. Extra arguments used when outputting the field.
 *
 *     @type string $label_for When supplied, the setting title will be wrapped
 *                             in a `` element, its `for` attribute populated
 *                             with this value.
 *     @type string $class     CSS Class to be added to the `` element when the
 *                             field is output.
 * }
 */
function add_settings_field($id, $title, $callback, $page, $section = 'default', $args = array()) {
	global $wp_settings_fields;
	if ( 'misc' == $page ) {
		_deprecated_argument( __FUNCTION__, '3.0', __( 'The miscellaneous options group has been removed. Use another settings group.' ) );
		$page = 'general';
	}
	if ( 'privacy' == $page ) {
		_deprecated_argument( __FUNCTION__, '3.5', __( 'The privacy options group has been removed. Use another settings group.' ) );
		$page = 'reading';
	}
	$wp_settings_fields[$page][$section][$id] = array('id' => $id, 'title' => $title, 'callback' => $callback, 'args' => $args);
}
更新版本源码位置使用被使用
4.2.0wp-admin/includes/template.php:158002

笔记(Notes)

带标签
我怀疑在标签的“id”属性中使用的可能被重写为在标签的“name”属性中使用的。
$id参数的描述是“在标记的’id’属性中使用”,但是这意味着您必须确保这个$id被用作与字段相关的输入元素的HTML id标记。WP仅使用此$id在其内部设置字段列表($WP_settings_field s)中为字段设置唯一的键。

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索