文章目录
本文最后更新于 2022-10-15,已超过 365 天未更新,内容可能已失效。

将新字段添加到“设置”页的某个部分。
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

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

CC 许可协议
署名-非商业性使用-相同方式共享 4.0 国际 了解协议
文章链接:https://zptheme.com/5483.html
本文作者:子佩·来源:子佩工作室

除非注明,子佩工作室 文章均为原创,转载请以链接形式标明本文地址。

相关推荐

发表回复

登录后即可发表评论

社交账号登录

全部评论 (0)

/