Skip Navigation

[Resolved] Create category per user

This support ticket is created 5 years, 8 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.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 3 replies, has 2 voices.

Last updated by Beda 5 years, 8 months ago.

Assisted by: Beda.

Author
Posts
#1233277

How can I accomplish this?

User creates account on frontend of site and enters their company name into a custom 'user meta field' I made with with your program. WordPress then takes that company name and creates a category with that same name for it, under posts>categories.

Thanks!

#1233377

It' cannot be accomplished without some custom PHP Code using the WordPress API and Forms API (I suppose the user submits a Toolset Form to create an account)

As first you'd have to intercept the Toolset Form with cred_save_data() which is the moment the form saves all new user data to the database.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

Then, within that hook, you'd get_user_meta() the value of the Field which the user filled out upon profile creation.
After, you'd use wp_insert_term() to add a term to the taxonomy "category".
https://codex.wordpress.org/Function_Reference/wp_insert_term

A sample (needs to be adapted to your case):

//hook our my_save_data_action() function to the cred_save_data hook of Toolset Forms.
//this makes the code following happening at the moment the form is saved
add_action('cred_save_data', 'my_save_data_action',10,2);

//Our custom function my_save_data_action
//It comes with 2 arguemtns, where: 
//$post_id is the ID of the post OR user currently submitted/edited with forms, 
//$form_data is an array of data related to the Form, see more on DOC page.
function my_save_data_action($post_id, $form_data)
{
    // if a specific form ID
    if ($form_data['id']==12)
    {
        //get data from user meta
        $user_field_value = get_user_meta($post_id, 'wpcf-user_field_slug, true);
        // add it to category
        wp_insert_term($user_field_value, 'taxonomy_name' );
        
    }
}

Please consider the above WordPress Codex and Toolset Doc for further technical details.

#1233450

Thanks for the sample. When I put this in my functions file, it brings down the website with this error:

Parse error: syntax error, unexpected 'taxonomy_name' (T_STRING), expecting ',' or ')' in /home/public_html/wp-content/themes/noteswebsite/functions.php on line 128

Line 128 is:
wp_insert_term($user_field_value, 'taxonomy_name' );

Any Ideas?

#1234165

As mentioned this code is a sample and won't work on your site.
Please consult WordPress's Codex for wp_insert_term() which I linked above:
https://codex.wordpress.org/Function_Reference/wp_insert_term

As you will notice, my code produced is a sample using dummy data, you need to adapt this on your site.
So the sample "wp_insert_term($user_field_value, 'taxonomy_name' );" becomes something applicable on your site, where you change taxonomy_name to a real taxonomy name, since of course taxonomy_name is not a real taxonomy on your site, but just a dummy text.

The same is valid for $user_field_value = get_user_meta($post_id, 'wpcf-user_field_slug', true); where you'll see that wpcf-user_field_slug needs to be changed to a real, existing field slug (prefix it with wpcf-) so it can work on your site.

Toolset Support does not produce tailored custom code, as per support policy, but we can give examples and review not working code.
https://toolset.com/toolset-support-policy/

The actual error you see is due to a typo on my sample, where I forgot a apostrophe on the line $user_field_value = get_user_meta($post_id, 'wpcf-user_field_slug, true);, which should feature a closing "'" apostrophe after 'wpcf-user_field_slug
Please accept my apologies for this typo.