1

Whenever I enter https://mydomain/wp-admin while not being logged in, I get redirected to

wp-login.php?redirect_to=https%3A%2F%2Fmydomain%2Fwp-admin%2F&reauth=1

With some help, I have created the following code to redirect users landing on the wp-login.php url to get forwarded to the desired custom page:

function redirect_wp_login_to_custom_login() {
    global $pagenow;
    $redirect_to;
    $current_url = home_url ($_SERVER ['REQUEST_URI']); 

    if ('wp-login.php' == $pagenow) {
        if (!is_user_logged_in ()) 
            $redirect_to = get_home_url () . '/login/';
        else {
            if (is_super_admin() || is_admin())
                $redirect_to = get_home_url () .  '/wp-admin/';
            else
                $redirect_to = get_home_url () .  '/startseite/';
        }
        wp_safe_redirect ($redirect_to);
        exit;
    }
    else {
        global $wp;
        $login_url = get_home_url () . '/login/';
        if (!strcasecmp ($login_url, $current_url) && is_user_logged_in ()) {
            $redirect_to = get_home_url () . '/user-profile/';
            //wp_safe_redirect ($redirect_to);
        exit;
        }
    }
}

However, when entering proper user credentials and clicking the "log in" button in my custom login form, I seem to be getting forwarded to wp-login.php. This makes my above function redirect me to the custom login form, because apparently after clicking "log in" I am not yet logged in. How can I resolve this (apart from writing a custom login function that gets executed right after the "log in" button is clicked?)

4
  • This sounds like a plugin you have installed is doing something. Try disabling all of the plugins first. Jun 3, 2021 at 1:41
  • No it's not. It is standard behavior of Wordpress. Jun 4, 2021 at 16:43
  • You know what, my apologies, I misread the URL entirely and it didn't register that that was the default WP redirect URL. Now that I'm reading this again, I actually have a custom built solution that I can provide if @Rodgath's answer below doesn't get what you need. Again, sorry for the confusion from my original comment. Jun 6, 2021 at 16:27
  • Hi Tony, absolutely no problem. I would be interested in seeing your solution. :) Jun 6, 2021 at 22:42

1 Answer 1

2

It's possible to redirect to any page of your site by using the 'init' hook. Here's an example code to show how it can be done.

add_action('init', 'wpse388876_redirect_to_home_page');
function wpse388876_redirect_to_home_page() {
    
    global $pagenow;

    if ('wp-login.php' == $pagenow && (!is_user_logged_in() || !is_super_admin() || !is_admin())) {
        
        $redirect_to = home_url();
        
        wp_safe_redirect($redirect_to);

        exit;
    }
}

All non-logged in users will be redirected but for logged-in users the super admin will not be redirected.

5
  • 1
    Thank you very much. I am just starting to dig into Wordpress' PHP interface, and this is very helpful. May 31, 2021 at 18:20
  • 1
    You are welcome. Remember you can change home_url() to the destination URL of your choice.
    – Rodgath
    Jun 1, 2021 at 7:58
  • After trying your code, I found that it does not quite do what I want it to. Your condition (!logged in || !super admin || !admin) also seems to be faulty, because a super_admin is not an admin and vice versa, so that condition will always yield true as result. I have come up with some modified code, but it has its own problems. I have edited my question accordingly. Jun 7, 2021 at 9:09
  • is_super_admin() is usually used if you have WP multi-site installed. You can remove if you haven't installed WP Multi-site. Moreover, I should point out that the conditional if ('wp-login.php' == $pagenow && (!is_user_logged_in() || !is_super_admin() || !is_admin())) { I have used in the answer is just an example, you can edit and alter it depending on your project.
    – Rodgath
    Jun 8, 2021 at 14:09
  • You may also try the code without the if conditional statement and see whether the redirection will work.
    – Rodgath
    Jun 8, 2021 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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