BuddyDev

Search

[Resolved] Add roles to users on new registration and first login

  • Keymaster
    (BuddyDev Team)
    Posts: 24250
    Brajesh Singh on #39269

    Hi Tosin,
    the code is working perfectly from me. I tried the following code(create a copy of Subscriber role and named volunteer)
    You ca see the role in Dashboard->Members-> 2nd column of the list

    
    /**
     * Change user role on first login
     */
    function buddydev_change_user_role_on_first_login( $user_login, $user ) {
    
    	if ( ! function_exists( 'buddypress' ) || user_can( $user->ID, 'manage_options' ) ) {
    		return;
    	}
    
    	// check for user's last activity.
    	$last_activity = bp_get_user_last_activity( $user->ID );
    
    	if ( empty( $last_activity ) ) {
    		$role = 'volunteer'; // please change with your desired role.
    		$user->set_role( $role );
    
    	}
    }
    
    add_action( 'wp_login', 'buddydev_change_user_role_on_first_login', - 1, 2 );
    
    

    I tried following use case.

    1. Registered a user via Dahsboard->User->New and logged, it changed correctly.
    2. Registered from front end using Auto Login and Auto Activation plugin, worked.
    3. Disabled Auto Activation plugin, registered a new account, activated using the key and logged, it worked too.

    For me, all the use cases are working. Please check the plugins or code that you might be using. They can have some effect on it.

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 886
    Tosin on #39279

    Hello Brajesh,

    I just found out why the code is not working, its conflicting with the other redirect code on first login. when I removed the redirect code it worked perfectly see code causing conflict.

     function buddydev_redirect_on_first_login( $user_login, $user ) {
    
    	if ( ! function_exists( 'buddypress' ) ) {
    		return;
    	}
    
    	// check for user's last activity.
    	$last_activity = bp_get_user_last_activity( $user->ID );
    
    	if ( empty( $last_activity ) ) {
    		update_user_meta( $user->ID, '_buddydev_first_login', 1 );
    		// Redirect you custom page.
    		bp_core_redirect( bp_get_root_domain() . '/welcome/' );
    	} elseif ( get_user_meta( $user->ID, '_buddydev_first_login', true ) ) {
    		delete_user_meta( $user->ID, '_buddydev_first_login' );
    		// Try second login here.
    		bp_core_redirect( bp_get_root_domain() . '/publish/' );
    	}
    }
    
    add_action( 'wp_login', 'buddydev_redirect_on_first_login', -1, 2 ); 

    How can this conflict be prevented.

    Thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 24250
    Brajesh Singh on #39280

    Hi Tosin,
    Thank you for sharing the details.

    You can simply change the line

    
    add_action( 'wp_login', 'buddydev_redirect_on_first_login', -1, 2 ); 
    

    to

    
    add_action( 'wp_login', 'buddydev_redirect_on_first_login', 0, 2 ); 
    

    and this should work.

    Please let me know if it works for you or not?

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 886
    Tosin on #39288

    This worked perfectly

    Thank you very much Brajesh, thanks to you my site is now 90% complete im really grateful

  • Keymaster
    (BuddyDev Team)
    Posts: 24250
    Brajesh Singh on #39295

    Hi Tosin,
    Thank you for confirming and the kind words. I am glad to be part of your success 🙂

    All the best!

    Regards
    Brajesh

The topic ‘ [Resolved] Add roles to users on new registration and first login’ is closed to new replies.

This topic is: resolved