我正在尝试尽可能自动地执行插件中每个设置的Settings API函数调用。循环遍历options数组,然后输出add_settings_section()
和add_settings_field()
很简单:
add_settings_section()
:
$oenology_hooks_tabs = oenology_hooks_get_settings_page_tabs();
foreach ( $oenology_hooks_tabs as $tab ) {
$tabname = $tab['name'];
$tabtitle = $tab['title'];
$tabsections = $tab['sections'];
foreach ( $tabsections as $section ) {
$sectionname = $section['name'];
$sectiontitle = $section['title'];
$sectiondescription = $section['description'];
// Add settings section
add_settings_section(
'oenology_hooks_' . $sectionname . '_section',
$sectiontitle,
'oenology_hooks_' . $sectionname . '_text',
'oenology_hooks_' . $tabname . '_tab'
);
}
}
和`add_settings_field():
global $oenology_hooks;
$oenology_hooks = oenology_hooks_get_hooks();
foreach ( $oenology_hooks as $hook ) {
$hookname = $hook['name'];
$hooktitle = $hook['title'];
$hooktab = $hook['tab'];
$hooksection = $hook['section'];
add_settings_field(
'oenology_hooks_' . $hookname,
$hooktitle,
'oenology_hooks_setting_callback',
'oenology_hooks_' . $hooktab . '_tab',
'oenology_hooks_' . $hooksection . '_section',
$hook
);
}
使用add_settings_field()
,我可以轻松地通过将$hook
变量传递给回调作为函数调用中的第五个参数来编写通用回调:
function oenology_hooks_setting_callback( $hook ) {
$oenology_hooks_options = get_option( 'plugin_oenology_hooks_settings' );
$hookname = $hook['name'];
$hooktitle = $hook['title'];
$hookdescription = $hook['description'];
$hooktype = $hook['type'];
$hooktab = $hook['tab'];
$hooksection = $hook['section'];
$inputvalue = $hookname . '_hide';
$inputname = 'plugin_oenology_hooks_settings[' . $inputvalue . ']';
$textareaname = 'plugin_oenology_hooks_settings[' . $hookname . ']';
$textareavalue = $oenology_hooks_options[$hookname];
if ( 'Filter' == $hooktype ) {
?>
<input type="checkbox" name="<?php echo $inputname; ?>" value="<?php echo $inputvalue;?>" <?php checked( true == $oenology_hooks_options[$inputvalue] ); ?> />
<span>Hide <?php echo $hooktitle; ?> content?</span><br />
<?php
}
?>
<span class="description"><?php echo $hooktype; ?> Hook: <?php echo $hookdescription; ?></span><br />
<textarea name="<?php echo $textareaname; ?>" cols="80" rows="3" ><?php
echo esc_textarea( $textareavalue );
?></textarea>
<?php
}
但是,似乎add_settings_section()
没有类似的$args
参数。因此,我不能使用相同的方法将$section
变量传递给回调。
因此,我的问题是:有什么方法可以将变量传递给add_settings_section()
回调,或者有其他方法可以创建类似于我正在做的回调的回调add_settings_field()
?
编辑:
@Bainternet钉了它!这是我的工作代码:
/**
* Callback for add_settings_section()
*
* Generic callback to output the section text
* for each Plugin settings section.
*
* @param array $section_passed Array passed from add_settings_section()
*/
function oenology_hooks_sections_callback( $section_passed ) {
global $oenology_hooks_tabs;
$oenology_hooks_tabs = oenology_hooks_get_settings_page_tabs();
foreach ( $oenology_hooks_tabs as $tab ) {
$tabname = $tab['name'];
$tabsections = $tab['sections'];
foreach ( $tabsections as $section ) {
$sectionname = $section['name'];
$sectiondescription = $section['description'];
$section_callback_id = 'oenology_hooks_' . $sectionname . '_section';
if ( $section_callback_id == $section_passed['id'] ) {
?>
<p><?php echo $sectiondescription; ?></p>
<?php
}
}
}
}
extract( $args, EXTR_SKIP );
以开头oenology_hooks_setting_callback()
,所以您不必将数组的每个部分保存到一个新的数组中$var
?然后$'name_inside_the_array
,这些将可用于,例如。“$标题,
$ tab`等