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
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
BrajeshThank 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
HiTosin,
you code is good except this lineget_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
BrajeshI 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
Hi Tosin,
What is active_member? Have you created a new role using some plugin?(active_member) is the new custom role I created using this plugin https://wordpress.org/plugins/members/
Hi Tosin,
I will test with that plugin tomorrow and will let you kow.Regards
Brajesh
The topic ‘ [Resolved] Add roles to users on new registration and first login’ is closed to new replies.