Skip Navigation

[Resolved] Adding a secondary role to a wordpress user

This support ticket is created 6 years, 3 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)

Author
Posts
#606582

Ive got seven types of users on my site,

Mechanic Technician Carpenter Painter

Supplier and Premium Supplier

What My registration form does is ask you to choose what type of supplier you are Mechanic Technician Carpenter Painter

It sets this choice as your role.

Then if you upgrade your package to premium you are granted a second role called "premium supplier" - this is done through Yith role changer with the woocommerce subscription. This bit works fine.

What I need is "supplier" to a secondary role until you upgrade.

I have tried this but using gravity forms user registration (I WANT TO USE THE TOOLSET REGISTRATION FORM)

add_action('gform_after_submission_31', 'my_update_user_role', 10, 2);
function my_update_user_role($entry) {

global $current_user;
get_currentuserinfo();
$user_id = $current_user->ID;

echo $user_id;
if(!$user_id)
return;

$user = new WP_User($user_id);
$user->add_role('supplier');
}
But all this does is remove the type of role Mechanic, Technician, Carpenter or Painter and replaces it with "supplier",

Any ideas how I can add the supplier role without removing one of the others.

I am using this plugin which has multiple role support hidden link

#606652

Dear Chantellel,

Here are my suggestions:
1) According to wordpress document:
https://codex.wordpress.org/Class_Reference/WP_User#add_role.28_.24role_.29
The method add_role should be able to "Add role to user", so I can not find anything wrong in your codes.
See similar thread of other website:
https://stackoverflow.com/questions/27420705/how-to-assign-multiple-roles-for-a-user-in-wordpress

2) If I am right, you are using "gravity form" plugin to setup the user role, and your question is a custom PHP codes problem, which is out the range of Toolset, see our support policy:
https://toolset.com/toolset-support-policy/
I suggest you ask help from author of "gravity form" plugin, check if it is possible with their plugin.

#607639

How can I do this using toolset?

I dont want to use gravity forms, But cant figure a way to do this using toolset

#607852

There isn't such a built-in feature within Toolset too, since in wordpress built-in profile page, the user's role is a select dropdown menu, it is a single choice option, you can not check if it is a user with multiple roles, but you can check it in the database.

I just tried it with CRED form for editing users, with CRED action hook "cred_save_data", similar as your custom codes:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==926)
    {
		$user_id = get_current_user_id();

		if(!$user_id)
		return;

		$user = new WP_User($user_id);
		$user->add_role('supplier');
		return;
    }
}

after submit the CRED form, It works fine, but you will need to check the result in the database, table "wp_usermeta", search the meta_key "wp_capabilities" of the edited user, you will get result, like this:

a:2:{s:11:"contributor";b:1;s:8:"supplier";b:1;}

There are two user roles in the "meta_value" column

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