设置API-创建可重复使用的表单元素?

时间:2012-02-09 作者:Wordpressor

我有一组设置字段:

(...)
add_settings_field( \'Option1\', \'Option 1\', \'textarea\', \'page1\', \'plugin_options\');  
add_settings_field( \'Option2\', \'Option 2\', \'textarea\', \'page1\', \'plugin_options\'); 
add_settings_field( \'Option3\', \'Option 3\', \'text_input\', \'page1\', \'plugin_options\');  
add_settings_field( \'Option4\', \'Option 4\', \'text_input\', \'page1\', \'plugin_options\');
(...)   
所有教程和Codex都指出,我必须为每个设置字段创建单独的渲染函数,但只为所有输入字段(如复选框、单选按钮、文本区域等)创建基本函数并重用它们不是更容易吗?

我相信如果我能够将字段id传递给这些函数,它将顺利工作,但我不确定如何传递?

function textarea() {
    $options = get_option(\'my_settings\');
    echo "<textarea id=\'[how to get ID?]\' name=\'my_settings[[how to get ID?]\'>{$options[\'how to get ID?\']}</textarea>";
}

1 个回复
最合适的回答,由SO网友:Chip Bennett 整理而成

您完全可以将可重用表单字段标记传递给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
}
?>
请注意,我考虑了“自定义”类型,以便可以为可能需要它的设置输出一次性标记。这些需要单独的回调。

此外,此代码还包含一个选项卡式设置页面,该页面可能比您需要的更复杂。不过,应该很容易将该位设置为静态。

结束

相关推荐

Give Children Unique ID's

有没有办法给每个wordpress子级(子类别UL列表)唯一的ID?我正在为子类别创建一个下拉列表,我想定位它们,但是:nth-child 尚未得到所有内容的支持。