add_settings_field

函数
add_settings_field ( $id, $title, $callback, $page, $section = 'default', $args = array() )
参数
  • (string) $id Slug-name to identify the field. Used in the 'id' attribute of tags.
    Required:
  • (string) $title Formatted title of the field. Shown as the label for the field during output.
    Required:
  • (callable) $callback Function that fills the field with the desired form inputs. The function should echo its output.
    Required:
  • (string) $page The slug-name of the settings page on which to show the section (general, reading, writing, ...).
    Required:
  • (string) $section Optional. The slug-name of the section of the settings page in which to show the box. Default 'default'.
    Required:
    Default: 'default'
  • (array) $args { Optional. Extra arguments that get passed to the callback function. @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. }
    Required:
    Default: array()
定义位置
相关方法
do_settings_fieldssettings_fieldsadd_settings_erroradd_settings_sectiondo_settings_sections
引入
2.7.0
弃用
-

add_settings_field: 这个函数用来向WordPress设置页面添加一个新的字段。你可以用这个函数在WordPress设置页上添加自定义字段和选项: 这个函数通常与add_settings_section一起使用,以分组相关的设置字段。

在设置页面的某个部分添加一个新的字段。

设置API的一部分。使用它来定义一个设置字段,它将作为设置页面内设置部分的一部分显示。这些字段将使用do_settings_fields()在do_settings_sections()中显示。

$callback参数应该是一个函数的名称,用于呼出这个设置字段的HTML输入标签。使用get_option()来检索要显示的现有值。

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',
			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';
	}

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

常见问题

FAQs
查看更多 >