查看函数的声明:
function add_settings_field(
$id,
$title,
$callback,
$page,
$section = 'default',
$args = array()
) { }
最后一个参数接受您的参数,并将其传递给回调函数。
我的插件公开联系数据示例
foreach ( $this->fields as $type => $desc )
{
$handle = $this->option_name . "_$type";
$args = array (
'label_for' => $handle,
'type' => $type
);
$callback = array ( $this, 'print_input_field' );
add_settings_field(
$handle,
$desc,
$callback,
'general',
'default',
$args
);
}
该函数print_input_field()
将这些参数作为第一个参数:
/**
* Input fields in 'wp-admin/options-general.php'
*
* @see add_contact_fields()
* @param array $args Arguments send by add_contact_fields()
* @return void
*/
public function print_input_field( array $args )
{
$type = $args['type'];
$id = $args['label_for'];
$data = get_option( $this->option_name, array() );
$value = $data[ $type ];
'email' == $type and '' == $value and $value = $this->admin_mail;
$value = esc_attr( $value );
$name = $this->option_name . '[' . $type . ']';
$desc = $this->get_shortcode_help( $type );
print "<input type='$type' value='$value' name='$name' id='$id'
class='regular-text code' /> <span class='description'>$desc</span>";
}
无需触摸全局变量。