Skip Navigation

[Resolved] Upgrade/change a user role form.

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

Problem:

I would like to create a form to change user's role after submitting the form.

e.g. If a customer fill in the form with driver's lic number and submit, the customer's role gets promoted to agent role.

Solution:

You can try action hook cred_save_data, like this:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
  
    if ($form_data['id']=='8853')
    {
        $u = new WP_User( $post_id );
        $u->set_role( 'business_owner' );
    }
}

Relevant Documentation:

https://developer.wordpress.org/reference/functions/wp_update_user/

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

Last updated by yosukeS 3 years, 8 months ago.

Assisted by: Luo Yang.

Author
Posts
#1679289

Tell us what you are trying to do?

I would like to create a form to change user's role after submitting the form.
e.g. If a customer fill in the form with driver's lic number and submit, the customer's role gets promoted to agent role.

Is there any documentation that you are following?

https://toolset.com/forums/topic/change-user-role-with-cred-form/
Do I just need to replace form ID and value "inventory_admin" on the code to my specific site?

Is there a similar example that we can see?

I have been using this but I would like to do the same thing inside of Toolset.
hidden link

What is the link to your site?

Please let me know.
Thank you!

#1679915

Hello,

In your case, you need to try these:
1) Replace form ID with your editing user form's ID
2) Check if the driver's lic number field is empty
3) Replace the "inventory_admin" with your custom user role slug, for example your custom user role's title is "my role", it should be "my_role"

#1681207

Hello Yang,

Thank you for the information.
After I applied your suggested steps.
I still cannot perform what I inteded to do.
After I submit the form, the page shows the message as I set at the form setting page. But the role is still the same as before.
I had a plug in conflict issue, so I disabled all other unrelated plugins and tried it again, but the code still doesn't work.

Could you help me on this litte more. I can provide the log in infor for the site.

#1681387

Please provide your website credentials in below private message box, also point out the:
1) The problem page URL, where I can submit the form
2) The post form URL
3) Where I can edit your PHP codes,
Thanks

#1683213

I have tried the credentials you provided above, it requires Two-Factor code, so I can not login your website, please check it, you can also setup another test site with same form settings, then provide the test site credentials in below private message box.

#1684491

I disabled 2FA now. Please try again.

#1684609

Thanks for the details, I have changed your PHP codes from:

    $current_user = wp_get_current_user();
    $user_id = $current_user->ID;
    wp_update_user ( array ( $user_id, 'role' => 'agent' ));

To:

		$u = new WP_User( $post_id );
		$u->set_role( 'agent' );

In Toolset user form, the first parameter is user's ID actually.

More help:
https://developer.wordpress.org/reference/functions/wp_update_user/

#1692309

Hi,
How do I add another code for form ID 8853 and user role "business_owner"?

#1692349

You can modify those codes like this:

...
if ($form_data['id']=='8853')
    {
		$u = new WP_User( $post_id );
		$u->set_role( 'business_owner' );
    }
...
#1707331

My issue is resolved now. Thank you!

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