Skip Navigation

[Resolved] Save custom user field to the database

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by Christian Cox 4 years ago.

Assisted by: Christian Cox.

Author
Posts
#1871043

Hi Team

I have just created a user registration form with a custom generic field.

The form works for all standard user fields but I have been unable to save the generic/custom user field to the database so I can use them later on user's my account page:

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']==101)
{
if (isset($_POST['current-role-in-tourism-industry-test']))
{
// add it to saved post meta
add_user_meta($user_id, '__current-role-in-tourism-industry-test', $_POST['current-role-in-tourism-industry-test'], true);
}
}
}

I would appreciate your help on this one?

Thanks

#1872331
Screen Shot 2020-12-10 at 3.58.55 PM.png

Hi, I'm not sure what the problem could be here, because I copy + pasted this exact code into a test site, added a generic Single Line field with the slug current-role-in-tourism-industry-test, modified the Form ID to match my New User Form, and it worked as expected. See the screenshot here, showing the new usermeta entry.
- Check to be sure your User Form's numeric ID is actually 101. If it is not, change the 101 in your code to match the User Form ID.
- If the code was added in Toolset > Settings > Custom Code, verify that the code snippet is activated and set to run everywhere.
- Double-check the slug of this generic field, and be sure it matches exactly current-role-in-tourism-industry-test.

If you're still having trouble, I may need to log into your site and take a closer look.

#1872487
wpse_users for the new user.PNG
wpse_usermeta screen.PNG
Elementor Template edit page.PNG
All users of WP.PNG

Hi Christian

First of all, thank you very much for your quick response and I really appreciate the great customer service and technical assistance you guys provide at Toolset. I work in the service industry myself and I know how important it is to be acknowledges for the good work you do every so often.

My understanding was the current-role-in-tourism-industry-test data should have appeared in the All Users link of the WP Admin panel along with the other user data collected in the form. Therefore, I thought that the current-role-in-tourism-industry-test field had not been saved to the database.

The other reason that I thought it had not been saved is when went to the wpse_users for the new user (davidraheem4), it did not list the current-role-in-tourism-industry-test field.

I can now see that it has saved the data under wpse_usermeta; however, when I try to use it with my Elementor Template edit page, it does not bring up this meta key option.

Do you know how I can actually use the current-role-in-tourism-industry-test in templates or anywhere else or and show this data on WP's users page on the admin panel?'

I am happy to allow you access to my website.

Thanks

#1874657
screen-options.png

My understanding was the current-role-in-tourism-industry-test data should have appeared in the All Users link of the WP Admin panel along with the other user data collected in the form. Therefore, I thought that the current-role-in-tourism-industry-test field had not been saved to the database.
In general this is not the case. The columns shown in this page are managed by WP, and are by default configured to show only a few standard fields like Username (mandatory), Name (mandatory), Email (optional), Role (optional), and Post count (optional). Those optional items can be turned on or off in Screen Options (see screen-options.png).

The other reason that I thought it had not been saved is when went to the wpse_users for the new user (davidraheem4), it did not list the current-role-in-tourism-industry-test field.
I can now see that it has saved the data under wpse_usermeta;
Correct. In the WordPress environment, user meta values are stored in the usermeta table, not directly in the users table.

Do you know how I can actually use the current-role-in-tourism-industry-test in templates or anywhere else or and show this data on WP's users page on the admin panel?
Since you've created your own custom usermeta key __current-role-in-tourism-industry-test to store arbitrary information in the usermeta table, I'm not really the best source of information about either of these things. Let me elaborate about where Toolset can help here.

When you use Types User Fields to store information in usermeta, Types can automatically display those Types User Fields in each User's Profile area of wp-admin. However, a custom usermeta value stored by implementing a generic field and custom programming is not the same as a Types User Field, and that information will not automatically appear in a User Profile. I can show you how to use a Types User Field in the Form instead, and eliminate the need for a generic field / custom usermeta key. But once you use a generic field to create your own custom usermeta key/value pair, displaying that field value in the wp-admin User Profile becomes a custom implementation that requires custom programming, e.g. https://wordpress.stackexchange.com/questions/119250/how-to-display-custom-user-meta-from-registration-in-backend

I'm not really the best source of information about using Elementor's templating system with custom usermeta values. That falls outside the scope of Toolset's involvement in the process, since this information isn't included in a Types User Field. At that point, it's a custom usermeta value that exists independently of Toolset, and Elementor would be able to give you more information about using this type of dynamic value in a template. I'm not sure whether or not it's possible, and don't want to give you misleading information.

#1875117

Hi Christian

Thanks for your response

Can you please show how I can use a Types User Field in the Form instead, and eliminate the need for a generic field / custom usermeta key. 

Thanks

Zaf

#1875141

Hi Christian,

I might probably be on the wrong track here.

What I am tryin to do is, in addition to Name, Surname and email address of a user, to collect info about profession and county of the user, through the Toolset registration form, and use this information in the user's my account page.

If you can advise how I, as someone with very limited coding skills, should go about it, I would really appreciate it?

Thanks
Zaf

#1875667

Sure, this is exactly what Toolset Types Custom User Fields are for, and require no extra programming. To create Types User Fields, go to wp-admin > Toolset > Custom Fields, and click the User Fields tab. Then you can create a Field Group to contain your fields, and begin adding the desired fields to that group.

After you add your fields in the User Field tab, you can edit your existing User Registration Form and insert those fields in the Form using the drag-and-drop editor. The fields are available in the Custom Fields panel. The fields you drag into the Form Builder will appear automatically in the front-end Registration Form, and the values each User inputs into those fields will automatically appear in their Profile.