^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 5年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云1核2g仅需102元/年,今日10点开抢

wordpress函数add_settings_field()用法示例

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


描述

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.


参数

$id

(string)
(Required)
Slug-name to identify the field. Used in the ‘id’ attribute of tags.

$title

(string)
(Required)
Formatted title of the field. Shown as the label for the field during output.

$callback

(callable)
(Required)
Function that fills the field with the desired form inputs. The function should echo its output.

$page

(string)
(Required)
The slug-name of the settings page on which to show the section (general, reading, writing, …).

$section

(string)
(Optional)
The slug-name of the section of the settings page in which to show the box.

Default value: ‘default’

$args

(array)
(Optional)
Extra arguments used when outputting the field.

  • ‘label_for’
    (string) When supplied, the setting title will be wrapped in a <label> element, its for attribute populated with this value.
  • ‘class’
    (string) CSS Class to be added to the <tr> element when the field is output.

Default value: array()


源代码

File: wp-admin/includes/template.php

function add_settings_field($id, $title, $callback, $page, $section = 'default', $args = array()) {
	global $wp_settings_fields;

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

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

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

更新日志

Version 描述
4.2.0 The $class argument was added.
2.7.0 Introduced.

相关函数

Uses

  • wp-includes/l10n.php:
    __()
  • wp-includes/functions.php:
    _deprecated_argument()

User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 4You must log in to vote on the helpfulness of this note

    Contributed by Codex

    With Label

    Adds a setting with id myprefix_setting-id to the General Settings page. myprefix should be a unique string for your plugin or theme. Sets a label so that the setting title can be clicked on to focus on the field.

    
    add_settings_field( 'myprefix_setting-id',
    	'This is the setting title',
     	'myprefix_setting_callback_function',
    	'general',
    	'myprefix_settings-section-name',
    	array( 'label_for' => 'myprefix_setting-id' ) );
    
    
  2. I suspect Used in the ‘id’ attribute of tags might be rewritten to Used in the ‘name’ attribute of tags.
    I think the $id param is used for identifying the field to be recognised by WP and to show the field or get that’s value. Whether to be used as actual tag’s attribute id‘s value or not depends on the circumstances (in $callback). Normally the name attribute might be taken for this aim.
    I just had been confused this param means to generate the attribute for some form element, but it seems not. When put ‘label_for’ in the $args that will be passed to the $callback, this generates label tag with attribute for automatically. So this value should be same as an actual id‘s value in the $callback which you write.

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈