Skip Navigation

[Resolved] Populate Select field for User Meta with values from View (or another CPT)

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

Problem:
Save CRED generic field into custom user field, for example:
generic field:
[cred_generic_field field='company' type='select' class='' urlparam='']
{
"required":0,
"validate_format":1,
"persist":1,
"default":[],
"options":[ [wpv-view name="cred-companies-as-json-list"] ]
}
[/cred_generic_field]

Save it into custom user field "user_company", which is created with Types plugin
Solution:
I suggest you try with CRED action hook cred_save_data to save the cred_generic_field to save it into your database.
https://toolset.com/documentation/user-guides/cred-api/#csd
This hook allows doing a custom action when post data is saved to database.

If you need update custom user field value, please try with WordPress function update_user_meta
https://codex.wordpress.org/Function_Reference/update_user_meta

For example:

add_action('cred_save_data', 'user_company_func',10,2);
function user_company_func($user_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==20)
    {
        if (isset($_POST['user_company']))
        {
            // add it to saved user meta
            update_user_meta($user_id, 'wpcf-user_company', $_POST['user_company'], true);
        }
    }
}

Relevant Documentation:
https://toolset.com/documentation/user-guides/cred-api/#csd

This support ticket is created 7 years, 10 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
- 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 6 replies, has 2 voices.

Last updated by Tristian 7 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#411874

When adding\editing a new user with CRED, I need to select a custom User Meta field called 'company'. The values are generated using a view.

This doesn't allow me to have a 'Select' field:

[cred_field field='company' post='user' value='' urlparam='']

This doesn't allow me to post as User meta for the custom user field 'Company':

[cred_generic_field field='company' type='select' class='' urlparam='']
{
"required":0,
"validate_format":1,
"persist":1,
"default":[],
"options":[ [wpv-view name="cred-companies-as-json-list"] ]
}
[/cred_generic_field]

Essentially I need a solution to select the company name from a drop-down list (generated by a view or some other way) and this be updated in the user meta field 'company'

Thanks for any help!?

#411958

Dear Tristian,

In the latest version of Views plugin, shortcode [wpv-layout-start] ... [wpv-layout-end] will output an extra div tag, so you can not the view's result as option of CRED cred_generic_field.

I suggest you try the solution in another thread:
https://toolset.com/forums/topic/i-need-the-ability-to-create-a-loop-without/#post-328540

#411964

Hi Luo,

Thanks - but that is not what I was asking. I have already solved this by removing the extra div with code in my functions.php file.

What I need to know is how to populate 'Select' field values for [cred_field field='company' post='user' value='' urlparam='']

I can get the values working in a JSON format for a cred_generic_field but not a cred_field.

Alternatively, to do things the other way round - how can I make a cred_generic_field save to a cred_field?

Essentially, I need to pre-populate Types Select fields?

Thanks

#412284

I suggest you try with CRED action hook cred_save_data to save the cred_generic_field to save it into your database.
https://toolset.com/documentation/user-guides/cred-api/#csd
This hook allows doing a custom action when post data is saved to database.

If you need update custom user field value, please try with WordPress function update_user_meta
https://codex.wordpress.org/Function_Reference/update_user_meta

#412299

Unfortunately I am not good at PHP. Would it be possible to provide the code to include in my functions file based on the following:

cred_save_data:
Cred User Form ID: 20
Post Type: User Form
Generic Field Name: user_company

update_user_meta:
$user_id: Record being created
$meta_key: user_company
$meta_value: value from user_company field in form

Would really be appreciated!

#412876

Here is the example codes:


add_action('cred_save_data', 'user_company_func',10,2);
function user_company_func($user_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==20)
    {
        if (isset($_POST['user_company']))
        {
            // add it to saved user meta
            update_user_meta($user_id, 'wpcf-user_company', $_POST['user_company'], true);
        }
    }
}
#412884

Thanks - this worked perfectly

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