I have a site where I want to update user info at login. This is important because the way a user interacts with the site is determined by information held in a CRM.
When a user logs in, I grab this info in a function that does an API call to the CRM. I then write the info to custom fields in the user profile and also update the user’s role.
I initially wrote the code using the wp_login hook. It worked in my dev environment but not the when I tried it in staging with the S2 plugin installed. I read here – S2member login wp-admin – that S2 overrides wp_login and that I should use wp_authenticate. I refactored the code for wp_authenticate and it tested good in dev, but again fails once it is in a site with S2.
Any Ideas of what I am missing? Is there another way to affect the user during the logon process?
function maximizer_login( $username ){
error_log ('Max Login Hook');
$user = get_user_by('login', $username);
if ($user != NULL) {
if( $user->has_cap( 'administrator') || $user->has_cap( 'editor')) {
} else {
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
$userstring = "user_".$user->id;
$userEmail = $user->user_email ;
error_log ("Max user ". $userstring . " email is: ". $userEmail );
$maxUserRole = get_maxi_user("$userEmail");
if ($maxUserRole != NULL) {
$user->set_role( $maxUserRole['userrole'] );
update_field('company_name', $maxUserRole['businessname'], $userstring);
update_field('company_id', $maxUserRole['businessid'], $userstring);
}
}
}
}
return $username;
}
add_action( 'wp_authenticate', 'maximizer_login' );