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 );\
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 );
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
Brajeshoops, I made a mistake. the above code will not work for sub sites. It is not aimed for that purpose.
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
BrajeshMulti Network isn’t enabled. Buddyboss is activated network wide on a normal multisite installation with the first blog as the home base.
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.