I have a function which kind of works. It will not work the first time when cookies are cleared. But the next time I will get redirected after login. Why is that?

function role_user_redirect_login($username, $user){
    if(array_key_exists('user', $user->caps)){
        wp_redirect(admin_url('edit.php?post_type=test', 'https'), 301);
add_action('wp_login', 'role_user_redirect_login', 10, 2);

You should use the filter login_redirect instead of wp_login:

function role_user_redirect_login($redirect_to, $request, $user){
    if(array_key_exists('user', $user->caps)){
        return admin_url('edit.php?post_type=test', 'https');

    return $redirect_to;

add_filter('login_redirect', 'role_user_redirect_login', 10, 3);
  • I tried this, and still end up in the "Your profile" screen. I have disabled access to the dashboard page for this user role (that's why I don't end up on the dashboard). If I log out and try to login again I will be redirected to the correct page. So it seems like when there are no cookies it will not redirect, but when I have cookies from the site it will work! – joq3 Jun 13 '18 at 6:50
  • Maybe another plugin or theme hooks into this. Could you try a higher priority? And i made a little mistake: it should be add_filter and not add_action. – ahendwh2 Jun 13 '18 at 8:13
  • I think I might have narrowed it down. I use this on a multisite. If I go to example.com/wp-admin and login, the redirect doesn't work (I end up on the Profile page). If I go to example.com/mysite/wp-admin and login the redirect works fine! So there is something missing in the redirection maybe? EDIT: I tried both your suggestions without any difference! The cookie problem I thought it was, was that when I logged out and tried again I was at example.com/mysite/wp-admin when doing the second attempt! – joq3 Jun 13 '18 at 8:24
  • It seems like the user gets redirected to example.com/wp-admin/edit.php at first, but has no access to this page and gets redirected again to profile screen. At the second attempt the user gets redirected to example.com/mysite/wp-admin/edit.php where he has access. So the redirect filter works fine, but the destination url is different – ahendwh2 Jun 13 '18 at 9:49
  • Can you provide me the URL of your site? – ahendwh2 Jun 13 '18 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.