isSelected’ => true
i have added this to items array but the last item of radio is begin selected. how to select the first one?
Can you share all your code please?
add_filter("gform_admin_pre_render", "populate_radio_buttons");
function populate_radio_buttons($form){
//only populating drop down for form id 5
if($form["id"] != 2)
return $form;
//Creating radio button item array.
$items = array();
$metas = get_post_meta( get_the_ID(), 'pc2', true );
if (is_array($metas))
{
foreach($metas as $meta) $items[] = array("value" => $meta['priced'], "text" => $meta['htlabel'], 'isSelected' => true);
}
//Adding items to field id 10. Replace 10 with your actual field id. You can get the field id by looking at the input name in the markup.
foreach($form["fields"] as &$field)
if($field["id"] == 37){
$field["choices"] = $items;
}
return $form;
}
add_filter( 'gform_display_add_form_button', function(){return false;} );
Hi Chandan,
The radio button field allows one selection at a time, and because what you currently have in the code is setting the isSelected property of all the choices to true, all the choices except the last one will be deselected. Here’s an updated version of your code that sets the isSelected property to true for only the first choice.
add_filter( 'gform_admin_pre_render', 'populate_radio_buttons' );
function populate_radio_buttons( $form ) {
//only populating drop down for form id 5
if ( $form['id'] != 34 ) {
return $form;
}
//Creating radio button item array.
$items = array();
$metas = get_post_meta( get_the_ID(), 'pc2', true );
if ( is_array( $metas ) ) {
foreach ( $metas as $meta ) {
if ( $metas [0] == $meta ) {
$items[] = array(
'value' => $meta['priced'],
'text' => $meta['htlabel'],
'isSelected' => true,
);
}
else{
$items[] = array(
'value' => $meta['priced'],
'text' => $meta['htlabel'],
);
}
}
}
//Adding items to field id 10. Replace 10 with your actual field id. You can get the field id by looking at the input name in the markup.
foreach ( $form['fields'] as &$field ) {
if ( $field['id'] == 37 ) {
$field['choices'] = $items;
}
}
return $form;
}
add_filter( 'gform_display_add_form_button', function() {
return false;
} );
I hope this helps.
Best,
1 Like
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.