0

When the user visits /wp-login.php and is already logged in, how can I redirect them to a custom URL? I found the same question asked and answered, only relating on who to redirect to the admin panel but I don't want to do that, I want to redirect to a specific URL.(wp-login.php -- redirect logged in users to custom URL).

I am specifically wanting to redirect them to my_home_URL.com/my-account How would I accomplish this? Any advice or guidance would be appreciated!

EDIT: I had little success but the below function when accessing wp-login.php it does successfully redirect to the URL I have set. However it does this redirect no matter if the user is logged in or not. How do I fix it so it successfully runs is_user_logged_in()?

function redirect_logged_in_user() 
{
    if( is_user_logged_in ) {
      wp_redirect('http://customurl');
    }
}

global $pagenow;
if( $pagenow == 'wp-login.php')     
redirect_logged_in_user();
1

Could be just a typo. Try writing:

is_user_logged_in()

Looks like just forgot the ()

UPDATE:

Because you don't want to redirect on every page load you have to hook your redirect. I here an example (taken and slightly edited from the docs):

<?php
function daro_login_redirect( $redirect_to, $request, $user ) {
    //is there a user to check?
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        //check for admins
        // edit this part accordingly to your login needs:
        // maybe you can skip the else statement
        if ( in_array( 'administrator', $user->roles ) ) {
            // redirect them to the default place
            return $redirect_to;
        } else {
            return home_url();
        }
    } else {
        return $redirect_to;
    }
}

add_filter( 'login_redirect', 'daro_login_redirect', 10, 3 );
  • Awesome thanks I got it all working. The only thing I now have occuring is that this is overriding admin users also being sent to that custom URL I have setup. Can I have it check the user's role and if they are and admin send them to the WP admin panel? – AppleTechy Dec 10 '17 at 3:20
  • Yes you can check the users role. Just one example I found googling: kellenmace.com/get-current-users-role-in-wordpress – Luckyfella Dec 10 '17 at 10:09

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.