Skip Navigation

[Resolved] Save value of user form generic field to custom user field

This thread is resolved. Here is a description of the problem and solution.

Problem:

Save Toolset Forms Generic field value into database.

Solution:

After user submit the form, you can get the field "avatar-select" value with PHP variable $_POST['avatar-select'], then save it into database, for example:

https://toolset.com/forums/topic/save-value-of-user-form-generic-field-to-custom-user-field/#post-1883509

Relevant Documentation:

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

This support ticket is created 4 years 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by rithvikU 4 years ago.

Assisted by: Luo Yang.

Author
Posts
#1883219
Screenshot 2020-12-22 at 6.40.22 AM.png

Tell us what you are trying to do?
hidden link
I am trying to create an option during user sign up to select one of nine images to be the user profile image. I know that I cannot update the native wordpress user profile image, so I followed the document mentioned below. I created a generic radio field in the create new user form and set the labels of each radio button to display the image through html. The value of each radio field is set to be the url of the image. I then created a custom image field in the toolset user fields. I need help using the cred_save_data hook and necessary wordpress APIs to save the value of the selected radio field into the custom user field for image.

Note: I have hidden the radio buttons using css . Also, I am a bit of a novice with coding, so if/when you suggest some code to add to my functions.php file, it would be great if you could provide notes on what variable I need to change to personalise the code for my site and where I can find them.

Is there any documentation that you are following?
https://toolset.com/forums/topic/insert-custom-photo-to-wp-profile-picture/
I need help adapting the code provided in that post for my particular use case.

What is the link to your site?
hidden link

#1883509

Hello,

After user submit the form, you can get the field "avatar-select" value with PHP variable $_POST['avatar-select'], then save it into database, for example:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($user_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==123) // here replace 123 with the user form's ID
    {
        $avatar_select = $_POST['avatar-select'];
        update_user_meta($user_id, 'avatar-select', $avatar_select); // here replace "avatar-select" with your custom user field slug
    }
}

If your custom user image field is created with Toolset Types plugin, you will need add "wpcf-" before the field slug, for example "wpcf-avatar-select"

#1883549
Screenshot 2020-12-22 at 1.59.30 PM.png
Screenshot 2020-12-22 at 1.47.19 PM.png
Screenshot 2020-12-22 at 1.44.12 PM.png

Hi Luo,

Thank you for the code and the explanation! I really thought this would work but there seems to still be some problem. My custom user field slug is 'wpcf-user-avatar', it is an image field. I modified the code as you suggested and added it to the functions.php file. When I tried registering a new user, the url (which I had assigned to the value of each radio option) isn't being saved in the custom user field. I have attached screenshots for your reference. Please help me figure out what I am doing wrong!

Edit: I have also attached a screenshot of the radio button code from the user sign up form, in case it helps

#1883613

In your user from, the generic field slug is "avatar-select", see your screenshot
https://toolset.com/wp-content/uploads/2020/12/1883549-Screenshot_2020_12_22_at_1.59.30_PM.png

Please replace the field slug from "avatar-select" with "wpcf-user-avatar", and test again

#1883709

Worked like a charm, I'd like to understand the logic behind that fix so please do share any resources that might help me learn. My issue is resolved now. Thank you so much Luo!