BuddyDev

Search

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

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

    Hello Brajesh,

    Please how can I add and identify users with custom roles when they register and login.
    I have already created the needed custom roles.

    Example
    1. When a new users registers add custom role (Non Active Member)
    2. When the same new registered users login for the first time add new custom role (Active Members) and remove former role (Non Active Member)

    This will help to identify spam users who have never loggedin. It will now be easier to identify and delete spam accounts from the (Non Active Member) section in users admin list

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

    Hi Tosin,

    1. You can set default role from Dashboard->settings->General screen and then updating the “New User Default Role” option.

    2. You do have the code for first login action, you can do a get_user_by(‘id’, $user_id)->set_role(‘Your custom role id’) to set this new role.

    e.g get_user_by(‘id’, $user_id)->set_role(‘subscriber’)

    Hope that helps.

    Regards
    Brajesh

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

    Thank you brajesh

    Please can you check the rough code

    Also the code should not affect administrators (Admins should be excluded)

     /**
     * Change user role on first login
     *
     */
    function buddydev_change_user_role_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 ) ) {
    		
    		get_user_by('id', $user_id)->set_role('active member');
    		
    	} 
    }
    add_action( 'wp_login', 'buddydev_change_user_role_on_first_login', -1, 2 ); 

    Thanks

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

    Gentle reminder

    Thanks

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

    HiTosin,
    you code is good except this line

    
    get_user_by('id', $user_id)->set_role('active member');
    

    I do not think role ids can have space. If you are using a plugin to create the role, Please use the role id and not the label. For example, “subscriber” is the role while “Subscriber” is the role label.

    Here is an example containing your code(modified).

    
    
    /**
     * 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 = 'subscriber'; // please change with your desired role.
    		$user->set_role( $role );
    
    	}
    }
    
    add_action( 'wp_login', 'buddydev_change_user_role_on_first_login', - 1, 2 );
    
    

    Regards
    Brajesh

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

    I tried the code but it did not change newly registered role on first login to active_member, I also tried using the code below but it also did not work

    
     if ( empty( $last_activity ) ) {
    		$active_role = 'active_member';
    		$non_active_role = 'non_active_member';
    		$user->remove_role( $non_active_role );
    		$user->add_role( $active_role );
    	} 
    

    I don’t know if this has any relevance

    wp_update_user

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

    Hi Tosin,
    What is active_member? Have you created a new role using some plugin?

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

    (active_member) is the new custom role I created using this plugin https://wordpress.org/plugins/members/

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

    Hi Tosin,
    I will test with that plugin tomorrow and will let you kow.

    Regards
    Brajesh

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

    Thank you Brajesh, I will be expecting your feedback.

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

This topic is: resolved