Radio buttons field

Select one option from several radio buttons.

Return value

The internal identifier for the selected field. This is a string that does not change if even you change to names or the order of the radio buttons.

Extended Return Values

Returns an array with useful information:

Array
(
[selected_value] => Radiobutton 2
[selected_radiobutton] => Array
(
[value] => Radiobutton 2
[key] => radiobutton_num_3
[is_selected] => 1
)
[radiobuttons] => Array
(
[0] => Array
(
[value] => Radiobutton 1
[key] => radiobutton_num_2
[is_selected] =>
)
[1] => Array
(
[value] => Radiobutton 2
[key] => radiobutton_num_3
[is_selected] => 1
)
[2] => Array
(
[value] => Radiobutton 3
[key] => radiobutton_num_4
[is_selected] =>
)
)
)
view raw gistfile1.php hosted with ❤ by GitHub

Example


// Field slug is "radiobuttonsExample"
// Returned value does not say anything about the order of the radio buttons.
// You have to print the values before knowing what value each radio button has.
// It's wierd. But it's done like that so you can change the order of the buttons
// and the names of the buttons without having to change your code.
$selected_value = simple_fields_value('radiobuttonsExample');
if ($selected_value == "radiobutton_num_3") {
    echo "You selected button two";
}

Output of above

You selected button two

Adding a field group with radiobuttons using PHP code


simple_fields_register_field_group('fg_radiobuttons_example',
	array (
		'name' => 'Radiobuttons example',
		'fields' => array(
			array(
				'slug' => "my_radiobuttons",
				'name' => 'Select yes or no',
				'type' => 'radiobuttons',
				'options' => array(
					"values" => array(
						array(
							"num" => 0,
							"value" => "Yes",
							"checked" => true,
						),
						array(
							"num" => 1,
							"value" => "No"
						),
						array(
							"num" => 2,
							"value" => "Maybe"
						),
					 )
				)
			),
		)
	)
);

Leave a Reply