How to use radio buttons in WordPress plugin options using register settings?

How do I get this to save the radio buttons? It saves the checkboxes but I’ve tried a lot of methods and can’t get it to save the radial buttons. Any help will be appreciated.

Working Plugin Example just drop into wp-content/plugins/myplug folder.

<?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
}
?>

Related posts

1 comment

  1. In the code you posted there are some errors, that maybe are only typos, in general the name of your inputs do not match with the name of the related setting.

    When you save your settings as array, name must be always in the form array_name['option_name'] but for radios you don’t follow this rule (but you do for others field).

    Using your code I’ve only changed myplug_validate and myplug_admin_page functions with:

    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 } ?>
    

    I’ve tested and it works perfectly.

Comments are closed.