BuddyDev

Search

How to auto assign a role on multisite buddypress (buddyboss) network

  • Participant
    Level: Initiated
    Posts: 14
    Mayzfieldtv on #41756

    I found this code in the buddydev forum. I have buddyboss network activated on the main site in a subsite network. When the member visits a subsite they become part of that subsite (that’s what I want), but they are not assigned any user role. Will this code safely assign the member an author role on the subsite that they visit?

    \/**
    * 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 = ‘author’; // please change with your desired role.
    $user->set_role( $role );

    }
    }

    add_action( ‘wp_login’, ‘buddydev_change_user_role_on_first_login’, – 1, 2 );\

  • Participant
    Level: Initiated
    Posts: 14
    Mayzfieldtv on #41757

    with backtick:

    /**
    * 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 = ‘author’; // please change with your desired role.
    $user->set_role( $role );
    
    }
    }
    
    add_action( ‘wp_login’, ‘buddydev_change_user_role_on_first_login’, – 1, 2 );
  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #41760

    Hi,
    Please use this code(there should not be any back tick inside the code, we use back tick here to wrap the code).

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

    That should work.

    Regards
    Brajesh

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #41761

    oops, I made a mistake. the above code will not work for sub sites. It is not aimed for that purpose.

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #41765

    Hi,
    I had a look and I don’t see BuddyPress/BuddyBoss adding the default role on sub site unless you are on the main site(or the Multi network mode is enabled). Do you have multi network mode enabled?

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 14
    Mayzfieldtv on #41785

    Multi Network isn’t enabled. Buddyboss is activated network wide on a normal multisite installation with the first blog as the home base.

  • Participant
    Level: Initiated
    Posts: 14
    Mayzfieldtv on #41786

    If I enable Multi-Network, it will work?

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #41789

    Hi,
    I will suggest finding out why the user is being added to the sub site. They should not be added to the sub site automatically. There must be some code or plugin doign it.

    enabling multi network for this purpose is not the right thing.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved