Set second role automatically
-
Hi
I’ve installed your plugin which works well
What I’m trying to do is automatically assign a second role (Author) whenever a user has an existing role of Customer.
Is this possible please?
Thanks
-
Hi,
I suppose that ‘Customer’ is assigned automatically by WooCommerce.
Go to the ‘Settings->User Role Editor->Default Roles’ tab and turn on ‘Author’ checkbox at the ‘Other default roles for new registered user’ list.Thanks Vladimir
Sadly that doesn’t work.
Whenever a new user signs up, the 2nd role isn’t being added automatically
I even tried the code from your website:
add_action( 'user_register', 'add_secondary_role', 10, 1 ); function add_secondary_role( $user_id ) { $user = get_user_by('id', $user_id); $user->add_role('author'); }However, that doesn’t seem to work either
Any further help is much appreciated!
Do you use the latest version of URE? There was a subject related bug in previous versions.
Thanks for the reply.
I’m using Version 4.19.3 which seems to be the latest one
I assume the code (above) can just go in the functions file? Surely that should work even if the plugin doesn’t?
‘Yes’ answer on the question about the code.
Please inform me, how user gets a ‘Customer’ role?
Is it your WordPress installed as multisite?Hi Vladimir,
I’ve predefined the customer role via WooCommerce
It’s not a multisite
What I’m trying to achieve is add a second role if the user has a particular capability, but can’t see how to do that.
Any help is much appreciated!
Forgot to add my code (sorry):
function add_secondary_role( $user_id ) { if (current_user_can('basic')) { $user = get_user_by('id', $user_id); $user->add_role('author'); } }So what I’d like to do, if a user has the capability of ‘basic’, it automatically adds the second role Author
However, with that in my functions file, it simply won’t work.
I can only get a second role to add when you do this by manually editing the user
Thanks
To what action did you link the function from the code above?
Hi Vladimir,
I had it wrapped in the following:
if ( current_user_can( 'basic' )) { function add_secondary_role( $user_id ) { if (current_user_can('basic')) { $user = get_user_by('id', $user_id); $user->add_role('author'); } } }Is there a better way of doing this or is this incorrect? Basically, I only want to apply this IF a user has a set capability (which is added via WooCommerce Groups)
Thanks
Apologies, I missed a line out:
if ( current_user_can( 'basic' )) { add_action( 'user_register', 'add_secondary_role', 10, 1 ); function add_secondary_role( $user_id ) { $user = get_user_by('id', $user_id); $user->add_role('author'); } }However, that doesn’t work either
Yes, code has a mistake.
You just declared a function, but never call it. So code inside your function is not executed. You don’t need a function if you don’t link it to the WP hook or call more than once.How much time you wish to try to add to the user with ‘basic’ capability the second role? I offer you to do it just in case user does not have ‘author’ role yet and user this simpler variant:
if (current_user_can('basic') && !current_user_can('author')) { global $current_user; $current_user->add_role('author'); }Modified version from you will not work as at the moment of user registration WP does not have logged in user, so the condition returns false always.
Try to remove external condition and make check inside your function:function add_secondary_role( $user_id ) { $user = get_user_by('id', $user_id); if (user_can($user, 'basic') { $user->add_role('author'); } }Sorry, I’ve now resolved this issue with the following code:
if ( current_user_can( 'basic' )) { function add_secondary_role( $user_id ) { $user_id = get_current_user_id(); $user = get_user_by('id', $user_id); $user->add_role('author'); } add_action( 'add_subscriber_role', 'add_secondary_role', 10, 2 ); do_action( 'add_subscriber_role', $user_id); }Thanks for your help!
The topic ‘Set second role automatically’ is closed to new replies.