2 comments

  1. If you look at canonical.php you will notice that the wp_redirect_admin_locations function is hooked very late– at priority 1000. This means that just about any function hooked to redirect_canonical runs before this one. So, conditionally remove wp_redirect_admin_locations from the template_redirect hook.

    add_action(
      'template_redirect', 
      function() {
        $requ = untrailingslashit($_SERVER['REQUEST_URI']);
        if (site_url('login','relative') === $requ ){
          remove_action( 'template_redirect', 'wp_redirect_admin_locations', 1000 );
        }
      }
    );
    
  2. This answer is based on the findings of “login” in permalink redirects to wp-login.php wp3.6

    Disable “/login” redirect

    As already stated there the relevant code is located at the very end in /wp-includes/canonical.php

    You need to remove the default “wp_redirect_admin_locations” and replace it with a slightly modified custom version. Add this to your function.php file.

    function custom_wp_redirect_admin_locations() {
        global $wp_rewrite;
        if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) )
            return;
    
        $admins = array(
            home_url( 'wp-admin', 'relative' ),
            home_url( 'dashboard', 'relative' ),
            home_url( 'admin', 'relative' ),
            site_url( 'dashboard', 'relative' ),
            site_url( 'admin', 'relative' ),
        );
        if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins ) ) {
            wp_redirect( admin_url() );
            exit;
        }
    
        $logins = array(
            home_url( 'wp-login.php', 'relative' )
        );
        if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins ) ) {
            wp_redirect( site_url( 'wp-login.php', 'login' ) );
            exit;
        }
    }
    
    function remove_default_login_redirect() {
        remove_action('template_redirect', 'wp_redirect_admin_locations', 1000);
        add_action( 'template_redirect', 'custom_wp_redirect_admin_locations', 1000 );
    }
    
    add_action('init','remove_default_login_redirect');
    

    Why this might not even be necessary

    Also you should be aware that the redirect only applies if there is nothing else at /login. As soon as e.g. you create a page /login the redirect doesn’t apply any more. (See /wp-includes/canonical.php:553 for where this happens.

Comments are closed.