您完全可以将可重用表单字段标记传递给add_settings_field()
. 诀窍是定义data type 对于每个设置,然后将相同的回调传递给add_settings_field()
. 在该回调中,只需添加switch 这包括每种数据类型的案例。
这是how I do it in Oenology:
首先,我动态地将所有调用输出到add_settings_field()
:
<?php
/**
* Call add_settings_field() for each Setting Field
*
* Loop through each Theme option, and add a new
* setting field to the Theme Settings page for each
* setting.
*
* @link http://codex.wordpress.org/Function_Reference/add_settings_field Codex Reference: add_settings_field()
*
* @param string $settingid Unique Settings API identifier; passed to the callback function
* @param string $title Title of the setting field
* @param callback $callback Name of the callback function in which setting field markup is output
* @param string $pageid Name of the Settings page to which to add the setting field; passed from add_settings_section()
* @param string $sectionid ID of the Settings page section to which to add the setting field; passed from add_settings_section()
* @param array $args Array of arguments to pass to the callback function
*/
foreach ( $option_parameters as $option ) {
$optionname = $option[\'name\'];
$optiontitle = $option[\'title\'];
$optiontab = $option[\'tab\'];
$optionsection = $option[\'section\'];
$optiontype = $option[\'type\'];
if ( \'internal\' != $optiontype && \'custom\' != $optiontype ) {
add_settings_field(
// $settingid
\'oenology_setting_\' . $optionname,
// $title
$optiontitle,
// $callback
\'oenology_setting_callback\',
// $pageid
\'oenology_\' . $optiontab . \'_tab\',
// $sectionid
\'oenology_\' . $optionsection . \'_section\',
// $args
$option
);
} if ( \'custom\' == $optiontype ) {
add_settings_field(
// $settingid
\'oenology_setting_\' . $optionname,
// $title
$optiontitle,
//$callback
\'oenology_setting_\' . $optionname,
// $pageid
\'oenology_\' . $optiontab . \'_tab\',
// $sectionid
\'oenology_\' . $optionsection . \'_section\'
);
}
}
?>
我是这样定义的
oenology_setting_callback()
:
<?php
/**
* Callback for get_settings_field()
*/
function oenology_setting_callback( $option ) {
$oenology_options = oenology_get_options();
$option_parameters = oenology_get_option_parameters();
$optionname = $option[\'name\'];
$optiontitle = $option[\'title\'];
$optiondescription = $option[\'description\'];
$fieldtype = $option[\'type\'];
$fieldname = \'theme_oenology_options[\' . $optionname . \']\';
// Output checkbox form field markup
if ( \'checkbox\' == $fieldtype ) {
?>
<input type="checkbox" name="<?php echo $fieldname; ?>" <?php checked( $oenology_options[$optionname] ); ?> />
<?php
}
// Output radio button form field markup
else if ( \'radio\' == $fieldtype ) {
$valid_options = array();
$valid_options = $option[\'valid_options\'];
foreach ( $valid_options as $valid_option ) {
?>
<input type="radio" name="<?php echo $fieldname; ?>" <?php checked( $valid_option[\'name\'] == $oenology_options[$optionname] ); ?> value="<?php echo $valid_option[\'name\']; ?>" />
<span>
<?php echo $valid_option[\'title\']; ?>
<?php if ( $valid_option[\'description\'] ) { ?>
<span style="padding-left:5px;"><em><?php echo $valid_option[\'description\']; ?></em></span>
<?php } ?>
</span>
<br />
<?php
}
}
// Output select form field markup
else if ( \'select\' == $fieldtype ) {
$valid_options = array();
$valid_options = $option[\'valid_options\'];
?>
<select name="<?php echo $fieldname; ?>">
<?php
foreach ( $valid_options as $valid_option ) {
?>
<option <?php selected( $valid_option[\'name\'] == $oenology_options[$optionname] ); ?> value="<?php echo $valid_option[\'name\']; ?>"><?php echo $valid_option[\'title\']; ?></option>
<?php
}
?>
</select>
<?php
}
// Output text input form field markup
else if ( \'text\' == $fieldtype ) {
?>
<input type="text" name="<?php echo $fieldname; ?>" value="<?php echo wp_filter_nohtml_kses( $oenology_options[$optionname] ); ?>" />
<?php
}
// Output the setting description
?>
<span class="description"><?php echo $optiondescription; ?></span>
<?php
}
?>
请注意,我考虑了“自定义”类型,以便可以为可能需要它的设置输出一次性标记。这些需要单独的回调。
此外,此代码还包含一个选项卡式设置页面,该页面可能比您需要的更复杂。不过,应该很容易将该位设置为静态。