Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Author Vladimir Garagulya

    (@shinephp)

    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.

    Thread Starter jarvo1980

    (@jarvo1980)

    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!

    Plugin Author Vladimir Garagulya

    (@shinephp)

    Do you use the latest version of URE? There was a subject related bug in previous versions.

    Thread Starter jarvo1980

    (@jarvo1980)

    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?

    Plugin Author Vladimir Garagulya

    (@shinephp)

    ‘Yes’ answer on the question about the code.
    Please inform me, how user gets a ‘Customer’ role?
    Is it your WordPress installed as multisite?

    Thread Starter jarvo1980

    (@jarvo1980)

    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!

    Thread Starter jarvo1980

    (@jarvo1980)

    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

    Plugin Author Vladimir Garagulya

    (@shinephp)

    To what action did you link the function from the code above?

    Thread Starter jarvo1980

    (@jarvo1980)

    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

    Thread Starter jarvo1980

    (@jarvo1980)

    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

    Plugin Author Vladimir Garagulya

    (@shinephp)

    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');
    }

    Plugin Author Vladimir Garagulya

    (@shinephp)

    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');
        }
    }

    Thread Starter jarvo1980

    (@jarvo1980)

    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!

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Set second role automatically’ is closed to new replies.