Get selected radio button value

Hi,
I’m lost on how to get a radio button selected value. I must be missing something but I can see how to get the values and the texts but not which one has been selected.
Thanks

To get it in the confirmation or notification, using the merge tag, you can use the “:value” modifier. If you are using the all fields merge tag you can do this:

{all_fields:value}

For individual fields, you can do this:

{Choose One:28:value} (that would work for field 28 where the field has a title Choose One)

Documentation (find “value” without the quotes):

If you are trying to get the value somewhere else, please let us know where you need to get it. Thank you.

I meant in the code?

When in the code? At what point in the form processing are you trying to access the selected value? Can you share with us what you’re trying to do? The more information the better. We’re happy to help point you in the right direction. Thank you.

I’m trying to assign the post format of a user generated post based on a radio button value.
function change_post_format( $post_data, $form, $entry ) {
//something here
}
add_filter( ‘gform_post_data’, ‘change_post_format’, 10, 3 );

Sure. You can use $selected_radio = rgar( $entry, '5' ) to get the value that was selected when the form was submitted. The code would look something like this:

add_filter( 'gform_post_data', 'change_post_format', 10, 3 );
function change_post_format( $post_data, $form, $entry ) {
	// Logging
	GFCommon::log_debug( __METHOD__ . '(): The entry => ' . print_r( $entry, true ) );
	GFCommon::log_debug( __METHOD__ . '(): Original post data => ' . print_r( $post_data, true ) );
	// assume radio button field is 5
	$selected_radio = rgar( $entry, '5' );
	if ( $selected_radio == 'gallery' ){
		// set the post format to gallery if the radio button value was gallery
		GFCommon::log_debug( __METHOD__ . '(): Radio button matched value gallery.' );
		$success = set_post_format( rgar( $entry, 'post_id' ), 'gallery' );
	}
	GFCommon::log_debug( __METHOD__ . '(): Possibly modified post data => ' . print_r( $post_data, true ) );
	return $post_data;
}

Thank you so much :slight_smile:

1 Like

Ok I had to make some changes but that worked. Apparently I can’t set the post format with gform_post_data…I was trying to use $post_data[‘post_format’] = ‘quote’ but doesn’t look like it’s available. I could change a post status, post content etc…not the format. So I did it with gform_after_submission and it works just great.
Thank you for your help.

1 Like

Thank you for the update.