Skip Navigation

[Resolved] Separate Radio Field Bullets

This support ticket is created 4 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

Tagged: 

This topic contains 9 replies, has 2 voices.

Last updated by Nashaat 4 years, 6 months ago.

Assisted by: Nigel.

Author
Posts
#1354737

I have created a custom field which i use in Toolset Form.

[cred_field field='winner-1' force_type='field' class='form-control' output='bootstrap']

This field has two options (1 / 2). is it possible to separate the radio bullets, so i can insert on different places in the form?

I tried to add the same field twice like this

[cred_field field='winner-1' force_type='field' class='form-control' output='bootstrap']
[cred_field field='winner-1' force_type='field' class='form-control' output='bootstrap']

It works and the values get saved, the only thing is that i have both options showing up and i need the first option only in the first field, while the second option show up on the second field

#1354747

This works with jQuery. i just wonder if it's possible with a different way
hidden link

#1355003

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

I think the least hacky way to do this is to add connected radio inputs wherever you want them in your form directly (i.e. in expert mode on the form write the actual input tags rather than inserting the input using a cred field shortcode) and then use the cred_save_data hook to get the field value from the global $_POST object and update the post meta yourself.

See this codepen for an example: hidden link

https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

#1355455

Hi Nigel,

I followed your suggestions and it works. can you confirm if i did everything the right way, would appreciate that!

1- added following code with my form ID

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12345)
    {
        if (isset($_POST['wpcf-winner-1']))
        {
            // add it to saved post meta
            add_post_meta($post_id, 'wpcf-winner-1', $_POST['wpcf-winner-1'], true);
        }
    }
}

2- Added the inputs to the form

<div class="first">Some stuff
<input type="radio" name="wpcf-winner-1" value="1">Option 1<br>
</div>
<hr>
<div class="second">Some other stuff
<input type="radio" name="wpcf-winner-1" value="2">Option 2<br>
</div>
</div>
#1355537

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

That's what I had in mind, yes, and I would expect it to work, which it does, yes?

#1355567

Yes! It's working now as expected. Thanks a lot Nigel

#1355975

Hi again,

I am having an issue with the "EDIT" form. It doesn't save the values of the custom Radio Buttons!
I have added the ID of the edit form to the function. but this isn't working unless i add the original input of cred form.
the create post form work as expected, this issue show up only when editing the post

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data){
  	$ids = array("12345","67890");
    // if a specific form
    if (in_array($form_data['id'], $ids) ){
        if (isset($_POST['wpcf-winner-1']))
        {
            // add it to saved post meta
            add_post_meta($post_id, 'wpcf-winner-1', $_POST['wpcf-winner-1'], true);
        }
      	if (isset($_POST['wpcf-winner-2']))
        {
            // add it to saved post meta
            add_post_meta($post_id, 'wpcf-winner-2', $_POST['wpcf-winner-2'], true);
        }
    }
}
#1355987

I forgot to mention that i am not using a content template for the form. instead i am using a page where i inserted the form manually based on the solution of Minesh here: https://toolset.com/forums/topic/content-template-custom-theme-support-for-toolset-forms/#post-1353801

Both ways are not saving the values of the custom inputs.

form in content template:
hidden link

form in a page:
hidden link

#1356681

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

The function add_post_meta will only add the custom field where it doesn't already exist, it won't overwrite an existing value where you are editing a post.

You would need to change the add_post_meta calls to the following format to work both to add or update a custom field:

if ( ! add_post_meta( $post_id, 'wpcf-winner-1', $_POST['wpcf-winner-1'], true ) ) { 
   update_post_meta ( $post_id, 'wpcf-winner-1', $_POST['wpcf-winner-1'] );
}
#1356879

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.