“login” in permalink redirects to wp-login.php wp3

2019-08-06 04:14发布

I'm trying to create a custom login page for a custom user page, but I can't get wordpress to stop redirecting the word login to wp-login.php.

Setup is pretty simple, I'm adding a rule that looks like this. account/login/?$ that yields to index.php?account=login. I also setup my rewrite tag for that custom query var. I have a couple other rules setup and they all work properly.

When I use monkey man's rewrite analyzer everything points properly, but on the front end any time I enter login into the url it redirects me to wp-login.php. It doesn't matter what segment it is entered at. so /login, /account/login, /dogs/bulldogs/login.... all redirect to wp-login.php

As far as I can tell there aren't any other rewrite rules containing login. So i'm at a loss.

As a last piece of info, i'm working in wp 3.6

Any advise or ideas would be greatly appreciated.

2条回答
虎瘦雄心在
2楼-- · 2019-08-06 04:27

Building on top of Diogos great answer, you can even disable the /login redirect without losing the other redirects. I just posted this solution on WPSE: Disable "/login" redirect

查看更多
贼婆χ
3楼-- · 2019-08-06 04:31

I'm facing the exact same problem. Google wasn't being very helpful, so I had the need to dig in. :)

Right on top of the wp-includes/template-loader.php file, you find this little snippet of code:

if ( defined('WP_USE_THEMES') && WP_USE_THEMES )
    do_action('template_redirect');

If you inspect what functions are hooked to that action at that time, you'll find a reference to the wp_redirect_admin_locations function (both the hook and the function are declared on the /wp-includes/canonical.php file - line 526).

Within the wp_redirect_admin_locations function, you'll find this:

 $logins = array(
      home_url( 'wp-login.php', 'relative' ),
      home_url( 'login', 'relative' ),
      site_url( 'login', 'relative' ),
  );
 if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins ) ) {
      wp_redirect( site_url( 'wp-login.php', 'login' ) );
      exit;
  }

An easy solution for the problem, would be to hook a function of yours to the *wp_redirect* filter. Something like this (within your functions.php):

add_filter('wp_redirect', 'yourprefix_filter_login_redirect', 10, 2);
function yourprefix_filter_login_redirect($location, $status)
{
     $uri = trim($_SERVER['REQUEST_URI'], '/');

      if ($uri === 'login') {
          return false;
      }

      return $location;
}

So... The *wp_redirect_admin_locations* terminates the script after trying to preform the redirect, so the solution above won't work.

The only other solution I'm seeing right now is to bypass the function all together which kind of sucks. All the other redirects defined within that function, will be lost all together...

Anyway, what I've ended up using:

    add_action(
          'init',
          function() {
              remove_action('template_redirect', 'wp_redirect_admin_locations', 1000);
          }
      );
查看更多
登录 后发表回答