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
Create each radio button and make sure they are under the same “group”. The following is a basic HTML version of what you want.
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
i tried using this but it doesent save my selection..
i guess i dont get where the var $size came from in this script?
My problem is that the radio buttons wont stay checked.. i tried using
the if / else solution.. but only one seletion stay checked..
(i know that logicly its weird)
My code is part of a widget i am building (wp widget)…
here is my code:
Use the same name for all the radio buttons that belong to the same group and use this to access the value.
For example, if you have a radio group called ‘gender’ with values ‘male’ and ‘female’, use male/female for all the get_field* calls except for get_field_name. Use ‘gender’ for the get_field_name.
In your widget() and update() code use ‘gender’ only – this will have either male or female as values.
For anyone struggles with javascript issue, wrong checked gets checked, make shure the HTML not have empty
checked=""
attributes. Only add the attribute on selected radio button.This code works for a widget with radio-buttons without if-statements: