WordPress函数文档add_settings_field()
注册添加一个设置选项项目 描述 Register a settings field to a settings…
注册添加一个设置选项项目
描述
Register a settings field to a settings page and section.
This is part of the Settings API, which lets you automatically generate wp-admin settings pages by registering your settings and using a few callbacks to control the output.
This function assumes you already know the settings $page
and the page $section
that the field should be shown on.
You MUST register any options used by this function with register_setting() or they won’t be saved and updated automatically.
The callback function needs to output the appropriate html input and fill it with the old value, the saving will be done behind the scenes.
The html input field’s name attribute must match $option_name in register_setting(), and value can be filled using get_option().
This function can also be used to add extra settings fields to the default WP settings pages like media or general. You can add them to an existing section, or use add_settings_section() to create a new section to add the fields to.
See Settings API for details.
用法
<?php add_settings_field( $id, $title, $callback, $page, $section, $args ); ?>
参数
$id
(string) (必填) String for use in the ‘id’ attribute of tags.
默认值: None
$title
(string) (必填) Title of the field.
默认值: None
$callback
(string) (必填) Function that fills the field with the desired inputs as part of the larger form. Passed a single argument, the $args array. Name and id of the input should match the $id given to this function. The function should echo its output.
默认值: None
$page
(string) (必填) The menu page on which to display this field. Should match $menu_slug from add_theme_page() or from do_settings_sections().
默认值: None
$section
(string) (可选) The section of the settings page in which to show the box (default or a section you added with add_settings_section(), look at the page in the source to see what the existing ones are.)
默认值: default
$args
(array) (可选) Additional arguments that are passed to the $callback function. The ‘label_for’ key/value pair can be used to format the field title like so: <label for=”value”>$title</label>.
默认值: array()
返回值
(void)
This function does not return a value.
示例
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.
1
2
3
4
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
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.7.0
源文件
add_settings_field() 函数的代码位于 wp-admin/includes/template.php
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
/**
* 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 `<label>` element, its `for` attribute populated
* with this value.
* @type string $class CSS Class to be added to the `<tr>` 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);
}
</tr></label>
|
相关
Settings API:
register_setting(),
unregister_setting(),
add_settings_field(),
add_settings_section(),
add_settings_error(),
get_settings_errors(),
settings_errors()
- 原文:http://codex.wordpress.org/Function_Reference/add_settings_field
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
评论功能已经关闭!