tags:

views:

26

answers:

1

How can I add a radio button in my wordpress widget form? I have been able to add input fields that gets saved and works fine. But I am having trouble with radiobuttons. Anyone?

This is my code for input field:

<p><label for="<?php echo $this->get_field_id('id'); ?>"><?php _e('Video ID:'); ?> 
    <input class="widefat" id="<?php echo $this->get_field_id('id');  ?>" 
    name="<?php echo $this->get_field_name('id'); ?>" 
    type="text" value="<?php echo $id; ?>" /></label></p>

and this is my radio button

<input type="radio" name="video_size" value="small"> Small<br>
    <input type="radio" name="video_size" value="full" checked> Full 
A: 

Create each radio button and make sure they are under the same "group". The following is a basic HTML version of what you want.

<input type="radio" name="group1" value="small" checked>Small<br />
<input type="radio" name="group1" value="full"> Full<br />

Take note of the name="group1" -- this groups the two selections together so you can choose one or the other. checked marks the default selection when the radio buttons load.

The WordPress version of this will require some logic to check what select the user has made. You will need to check if small or full is selected and have it supplement the checked attribute accordingly.

For instance

if($size == 'Full')
{
     echo '<input type="radio" name="group1" value="small">Small<br />' . "\n";
     echo '<input type="radio" name="group1" value="full" checked> Full<br />' . "\n";
     //This will out put the radio buttons with Full chosen if the user selected it previously.
}
else
{
     echo '<input type="radio" name="group1" value="small" checked>Small<br />' . "\n";
     echo '<input type="radio" name="group1" value="full"> Full<br />' . "\n";
     //This will out put the radio buttons with Small selected as default/user selected.
}
hsatterwhite
thanks, thats great..I cant get it to save to the database though, any ideas?
vick
Hmm.. the selection will be stored in to $_POST['group1'] in my example, so you could try checking that variable. Also take a look at a real world example of a PHP form processing a radio button selection: http://apptools.com/phptools/forms/forms3.php
hsatterwhite