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.

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']);

/** Draw the Settings Page **/
function myplug_admin_page(){
  <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(); ?>

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() { ?>
  <form method="post" action="options.php">
  $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(); ?>
<?php } ?>

I've tested and it works perfectly.

  • Thanks! I can't believe I didn't notice the name field. It might have taken me a week or two to catch it since once I get stumped I tend to look at stuff only a few times a day until I hit nirvana. Again thanks, I really appreciate it. BTW I really like that switch in the validation function. Unfortunately there are many more validation rules in the real function so that I can't use it. But you have a very good eye for code. Good Job! – Mark Finch Sep 12 '13 at 14:37
  • 1
    Thanks @MarkFinch! I was sure that the array_map trick can't be used in real world, but for test pourpose it's perfect! ;) – gmazzap Sep 12 '13 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.