Using WordPress – I want to check who has just logged in and if they are UsernameX then redirect to PageX, if they are an Administrator then redirect to /wp-admin/ and all others redirect to home_url();
This is my code:
function my_login_redirect($redirect_to, $request, $user){
if(in_array('student',$user->user_login)) {
return home_url("/students/");
} else {
if(in_array('administrator', $user->roles)){
return home_url("/wp-admin/");
} else {
return home_url();
}
}
}
add_filter("login_redirect", "my_login_redirect", 10, 3);
However it doesn’t seem to work – particularly the check against user_login. Is there a better – working – way to do this?
Answer / Correct code – thanks to @soju:
function my_login_redirect($redirect_to, $request, $user){
if($user->first_name == 'student')) {
return home_url("/students/");
} else {
if(in_array('administrator', $user->roles)){
return home_url("/wp-admin/");
} else {
return home_url();
}
}
}
add_filter("login_redirect", "my_login_redirect", 10, 3);
This means using the User’s first name as $user doesn’t contain the username, but if you wanted you could get it using the ID as returned by $user I guess.
This line :
is not correct,
$user->user_logindoes not exist and is not an array.EDIT : http://codex.wordpress.org/Class_Reference/WP_User