如何通过寄存器设置在WordPress插件选项中使用单选按钮?

时间:2013-09-11 作者:Mark Finch

如何获取此项以保存单选按钮?它可以保存复选框,但我尝试了很多方法,但无法保存径向按钮。任何帮助都将不胜感激。

工作插件示例只需放入wp-content/plugins/myplug文件夹。

<?php
/*
Plugin Name: myplug
Version: 0.1
Plugin URI: http://myplug.org
Author: ME
Description: Stupid plugin
*/

add_action(\'admin_init\', \'myplug_register_options\');  // register options for the form
add_action(\'admin_menu\', \'myplug_admin_links\');  // register admin menu hyperlinks

/** Function to register form fields **/
function myplug_register_options(){
    register_setting(\'myplug_options_group\', \'myplug_settings\', \'myplug_validate\');
}

/** Function to add hyperlinks to the admin menus using hooks and filters. **/
function myplug_admin_links() {
  add_options_page(\'myplug Setup\', \'myplug\', \'manage_options\', \'myplug\', \'myplug_admin_page\' );  // add link to settings page
  add_filter( \'plugin_action_links\', \'myplug_settings_link\', 10, 2 );  // add link to plugin page
}

/** Function to create link for plugin_action_links filter **/
function myplug_settings_link($links, $file){
    if ( $file == plugin_basename( dirname(__FILE__). \'/myplug.php\')){
            $settings_link = \'<a href="options-general.php?page=myplug">\' .__(\'Settings\') . \'</a>\';
            array_unshift( $links, $settings_link ); // place before other links
    }
return $links;
}

/** Validate User Input **/
function myplug_validate($input) {
    $input[\'value1\'] = wp_filter_nohtml_kses($input[\'value1\']);
    $input[\'value2\'] = wp_filter_nohtml_kses($input[\'value2\']);
    $input[\'select_display\'] = wp_filter_nohtml_kses($input[\'select_display\']);
return($input);
}

/** Draw the Settings Page **/
function myplug_admin_page(){
?>
<div>
  <h2>Options</h2>
  <form method="post" action="options.php">
  <?php settings_fields(\'myplug_options_group\'); ?>
  <?php $myplug_options = get_option(\'myplug_settings\'); ?>
        <input type="checkbox" name="myplug_settings[select_value1]" value="1" <?php checked(\'1\', $myplug_options[\'select_value1\']); ?> />
        <label style="display:inline-block;width:100px;">value1:</label>
        <input style="display:inline-block;width:300px;" type="text" name="myplug_settings[value1]" value="<?php echo $myplug_options[\'value1\']; ?>" /><br />
        <input type="checkbox" name="myplug_settings[select_value2]" value="1" <?php checked(\'1\', $myplug_options[\'select_value2\']); ?> />
        <label style="display:inline-block;width:100px;">value2:</label>
        <input style="display:inline-block;width:300px;" type="text" name="myplug_settings[value2]" value="<?php echo $myplug_options[\'value2\']; ?>" /><br />
        <?php echo \'display:&nbsp;&nbsp;\' . $myplug_options[\'select_display\'] . \'<br />\'; ?>
        <input type="radio" name="select_display" id="item1" value="item1"     <?php if($myplug_options[\'select_display\'] == \'item1\') echo \'checked="checked"\'; ?> />
        <input type="radio" name="select_display" id="item2" value="item2"     <?php if($myplug_options[\'select_display\'] == \'item2\') echo \'checked="checked"\'; ?> />
  <?php submit_button(); ?>
  </form>
</div>
<?php
}
?>

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

您发布的代码中有一些错误,可能只是输入错误,通常输入的名称与相关设置的名称不匹配。

将设置保存为数组时,名称必须始终位于表单中array_name[\'option_name\'] 但对于收音机,你不遵守这个规则(但对于其他领域,你会遵守)。

使用你的代码我只更改了myplug_validatemyplug_admin_page 具有以下功能:

function myplug_validate($input) {
  return array_map(\'wp_filter_nohtml_kses\', (array)$input);
}

function myplug_admin_page() { ?>
<div>
  <h2>Options</h2>
  <form method="post" action="options.php">
  <?php
  settings_fields(\'myplug_options_group\');
  $myplug_options = get_option(\'myplug_settings\');
  ?>
  <input type="checkbox" name="myplug_settings[checkbox1]" value="1" <?php checked(\'1\', $myplug_options[\'checkbox1\']); ?> /><br />
  <input type="text" class="regular-text" name="myplug_settings[text1]" value="<?php echo $myplug_options[\'text1\']; ?>" /><br />
  <input type="checkbox" name="myplug_settings[checkbox2]" value="1" <?php checked(\'1\', $myplug_options[\'checkbox2\']); ?> /><br />
  <input type="text" class="regular-text" name="myplug_settings[text2]" value="<?php echo $myplug_options[\'text2\']; ?>" /><br />
  <input type="radio" name="myplug_settings[radio1]" value="item1" <?php checked(\'item1\', $myplug_options[\'radio1\']); ?> /><br />
  <input type="radio" name="myplug_settings[radio1]" value="item2" <?php checked(\'item2\', $myplug_options[\'radio1\']); ?> /><br />     
  <?php submit_button(); ?>
  </form>
</div>
<?php } ?>
我已经测试过了,效果很好。

结束

相关推荐

Sliders with buttons

我正在寻找一个滑块插件,它允许我在滑块中插入其他按钮,而不是下一个和上一个数字,我的意思是将用户重定向到不同页面的按钮,等等。。。我会很感激你的信息!当做