Hello, I am working with User Forms and Custom User Fields. Is there a way to assign a value to a User Field depending on the value of a different User Field?
EXAMPLE
Form: my-user-form
Field displayed to user within my-user-form: my-favorite-animal-field (radio field)
Hidden field: my-personality-field (radio field?)
LOGIC TO ASSIGN VALUE FOR HIDDEN FIELD (After user submits value for above displayed field):
If my-favorite-animal-field = “dog"
Then my-personality-field = “loyal"
If my-favorite-animal = “cat"
Then my-personality-field = “independent"
CODE PLACEMENT
Also please specify whether I can insert code for the above within the form itself and/or within the wordpress post.
Hi,
Thank you for contacting us and I'd be happy to assist.
To programmatically update the value of one user field, based on the other one, you can use custom function attached to the "cred_save_data" hook:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
For example:
add_action('cred_save_data', 'custom_function_user_form',10,2);
function custom_function_user_form($new_user_id, $form_data)
{
if ($form_data['id']==1234)
{
if($_POST['wpcf-my-favorite-animal-field'] == 'dog') {
update_user_meta($new_user_id, 'wpcf-my-personality-field', 'loyal');
}
elseif($_POST['wpcf-my-favorite-animal-field'] == 'cat') {
update_user_meta($new_user_id, 'wpcf-my-personality-field', 'independent');
}
}
}
You'll replace "1234" with the actual form's ID.
The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.
Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar
Thank you!
1) So this is what I think the form is doing - please correct me if needed...
As soon as form #1234 has been submitted, check to see what the user selected as their favorite animal. If they selected dog, then assign a value of “loyal” to the personality field. If they go back to that form later and change their response to cat, a value of "independent" will overwrite the value of the personality field.
2) What do the 10 and the 2 mean in the above code, and would I ever need to change these values?
3) If the field is a different type - radio field vs checkbox, etc... would I need to change anything?
Thanks for writing back and sorry about the delay in getting back on this.
1) So this is what I think the form is doing - please correct me if needed...
As soon as form #1234 has been submitted, check to see what the user selected as their favorite animal. If they selected dog, then assign a value of “loyal” to the personality field. If they go back to that form later and change their response to cat, a value of "independent" will overwrite the value of the personality field.
- Yes, your understanding is correct and the suggested code snippet will work for both types of forms; i.e. for add new user form and for editing existing user form.
2) What do the 10 and the 2 mean in the above code, and would I ever need to change these values?
- In the "add_action" function, the third parameter (10) represents priority and the fourth parameter (2) represents the number of arguments accepted by the connected function.
( ref: https://developer.wordpress.org/reference/functions/add_action/ )
When using this "add_action" function with the "cred_save_data" hook, it is very less likely that you'll need to change these two values.
3) If the field is a different type - radio field vs checkbox, etc... would I need to change anything?
- The Toolset Types plugin stores the "checkboxes" types custom field data in a special serialized form. I'm afraid, there is no built-in method/feature available to programmatically update the value of "checkboxes" types custom field, so in all such cases, it is better to use "radio" type custom fields.