简体   繁体   English

禁用wp-admin到wp-login规范重定向(wordpress)

[英]disable wp-admin to wp-login canonical redirect (wordpress)

I am building a website, with wordpress+buddypress (latest version). 我正在建立一个网站,使用wordpress + buddypress(最新版本)。

In this website, I have my own custom login|signup|resetpass forms, and I do not want to link them to back-end wp-forms. 在这个网站上,我有自己的自定义登录|注册|重置通道表单,我不想将它们链接到后端的wp-forms。

I have blocked the back-end forms for all users (default wp-login|signup|resetpass forms)(with a 404 header code) 我已阻止所有用户的后端表单(默认wp-login |注册|重置表单)(带有404标头代码)

So if you try to reach wp-admin/wp-login you will see the 404. 因此,如果您尝试访问wp-admin / wp-login,您将看到404。

I do not want to use any kind of redirection I want to stop the redirection on a special URL, as to Stop the redirection from that URL to any other URLs I want to stop redirection from /wp-admin to /wp-login.php?redirect_to=http%3A%2F%2Fsite.com%2Fwp-admin%2F&reauth=1 我不想使用任何类型的重定向 我想停止特殊URL上的重定向,以停止从该URL重定向到 我想要停止从 /wp-admin 重定向/wp-login.php?redirect_to=http%3A%2F%2Fsite.com%2Fwp-admin%2F&reauth=1 任何其他URL /wp-login.php?redirect_to=http%3A%2F%2Fsite.com%2Fwp-admin%2F&reauth=1

When you try to reach www.example.com/wp-admin and you are not logged in, you will automatically get redirected to: 当您尝试访问www.example.com/wp-admin并且您尚未登录时,您将自动重定向到:

example.com/wp-login.php?redirect_to=http%3A%2F%2Fsite.com%2Fwp-admin%2F&reauth=1

you will get redirected... the URL will change automatically this is the default action of wordpress. 您将被重定向... URL将自动更改 这是wordpress的默认操作。

I want to stop that automatic redirection . 我想停止自动重定向

When you try to reach /wp-admin, you have to stay at /wp-admin (you should not get a redirect to wp-login). 当你尝试访问/ wp-admin时,你必须留在/ wp-admin(你不应该重定向到wp-login)。

Please see these 2 pictures for full details : 请查看这两张图片了解详情

Picture 1: 图片1:

example.com/wp-admin

Picture 2: 图2:

你看到网址自动更改了

* NOTE : *注意

The 404 is something I made to happen..., it's not an error. 404是我做的事情......,这不是错误。

I've tested so many codes, to stop it. 我已经测试了很多代码,以阻止它。 but none worked for me. 但没有一个对我有用。

Code number 1: 代码1:

remove_action('template_redirect', 'redirect_canonical');

Code number 2: 代码2:

remove_filter('template_redirect', 'redirect_canonical');

Code number 3: 代码3:

add_action(
    'init',
    function () {
        remove_action('template_redirect', 'wp_redirect_admin_locations', 1000);
    }
);

Code number 4: 代码4:

function custom_wp_redirect_admin_locations() {
    global $wp_rewrite;
    if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) )
        return;
    $admins = array(
        home_url( 'wp-admin', 'relative' ),
    );
    if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins ) ) {
        $wp_query->set_404();
        get_template_part( 404 ); 
        exit();
    }
    $logins = array(
        home_url( 'wp-login.php', 'relative' )
    );
    if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins ) ) {
        $wp_query->set_404();
        get_template_part( 404 ); 
        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');

Code number 5 : 代码5:

add_action(
    'template_redirect',
    function () {
        $requ = untrailingslashit($_SERVER['REQUEST_URI']);
        if (site_url('wp-admin', 'relative') ===
        untrailingslashit($_SERVER['REQUEST_URI'])) {
            remove_action('template_redirect', 'wp_redirect_admin_locations', 1000);
        }
    }
);

Code number 6: 代码6:

function custom_wp_redirect_admin_locations() {
    global $wp_rewrite;
    if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) )
        return;

    $requested_url = untrailingslashit( $_SERVER['REQUEST_URI'] );

    $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 ) ) {
        redirect_canonical( $requested_url , false );
        exit;
    }

    $logins = array(
        home_url( 'wp-login.php', 'relative' )
    );
    if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins ) ) {
        redirect_canonical( $requested_url , false );
        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');

Whatever I used doesn't seem to be doing the thing I want. 无论我用什么似乎都没有做我想要的事情。 How can I do it? 我该怎么做?


EDIT : Clarify the context, which is not a duplicate. 编辑:澄清上下文,这不是重复。

The link is said as duplicate clearly saying that he wants his wp-login pages to get redirect to his own login/registration pages. 链接被称为重复 明确表示他希望他的wp-login页面重定向到他自己的登录/注册页面。

But I clearly said "Please read the first 16 lines". 但我清楚地说“请阅读前16行”。 which means that I do not try/want to redirect + I do not try/want to restrict using any kind of redirection 这意味着我不尝试/想要重定向 + 我不尝试/想要限制使用任何类型的重定向

Plus: I've read all of these articles before posting this. 另外:我在发布之前已经阅读了所有这些文章。 But none of them were the thing that I wanted. 但它们都不是我想要的东西。 So I started a new topic , with full detail. 所以我开始了一个全新细节的新主题。

FYI, I have read 7 more articles (which I picked the codes from them): 仅供参考,我已经阅读了7篇文章(我从中挑选了代码):

But I need at least 10reputations to post more than 2 links! 但我需要至少10个声明才能发布2个以上的链接! So I cannot say more than 2. 所以我不能说超过2个。

  1. the second article 第二篇文章
  2. the third article 第三篇文章
  3. the fifth article 第五篇文章

I am grateful for your time and attention, but please read my question one more time with more attention. 我很感激你的时间和精力,但请再多一点关注我的问题。

First - explanation. 首先 - 解释。

Wordpress is kind of tricky, when it comes to admin pages. 当涉及管理页面时,Wordpress有点棘手。 Essentially, when admin page is being loaded, wp-admin/admin.php is being included. 基本上,当加载管理页面时,包含wp-admin/admin.php Inside this file there is a call to a function called auth_redirect() It checks, if user is logged in, and if not - redirects him to a login page. 在这个文件里面有一个名为auth_redirect()的函数调用它会检查用户是否已登录,如果没有,则将其重定向到登录页面。

As this function is not a typical action/filter, it is kind of hard to disable it. 由于此功能不是典型的动作/过滤器,因此很难禁用它。 Fortunately, it calls several hooks on its own. 幸运的是,它自己调用了几个钩子。 One of them, auth_redirect_scheme , is called just before real redirection happens. 其中之一auth_redirect_scheme在真正的重定向发生之前被调用。 It is meant to prepare a 'scheme' (http/https) for redirection, but we can exploit it to suit your goals. 它旨在为重定向准备一个'方案'(http / https),但我们可以利用它来满足您的目标。

I added a filter hook for auth_redirect_scheme , with priority 9999 (it does not really matter, but I wanted it to run late, just in case). 我为auth_redirect_scheme添加了一个过滤器钩子,优先级为9999(这并不重要,但我希望它能延迟运行,以防万一)。 I then took a piece of code from original auth_redirect() used to check, if user is logged in ( wp_validate_auth_cookie ). 然后,我从原始auth_redirect()了一段代码,用于检查用户是否已登录( wp_validate_auth_cookie )。 If he is, we just return value, as nothing has to be done. 如果他是,我们只是返回价值,因为没有必要做。 If the user is not logged in, though, we show an error page and exit the script (to prevent redirect of happening). 但是,如果用户未登录,则会显示错误页面并退出脚本(以防止重定向发生)。

Also, just in case I disabled wp_redirect_admin_locations filter. 另外,以防万一我禁用了wp_redirect_admin_locations过滤器。 I'm not really sure, if this is needed, but... 我不确定,如果需要的话,但......

And now - the code. 现在 - 代码。 Mind, this might not be the perfect solution and will require some improvements from your part. 请注意,这可能不是完美的解决方案,需要您的一些改进。

<?php
/**
 * @packageStop_Redirect
 */
/*
Plugin Name: Stop redirect
Plugin URI: 
Description: Stop redirecting anything to wp-login
Author: Tomasz Struczyński
Version: 0.1
Author URI: 
*/

add_action('init', 'remove_default_redirect');
add_filter('auth_redirect_scheme', 'stop_redirect', 9999);

function stop_redirect($scheme)
{
    if ( $user_id = wp_validate_auth_cookie( '',  $scheme) ) {
        return $scheme;
    }

    global $wp_query;
    $wp_query->set_404();
    get_template_part( 404 );
    exit();
}

function remove_default_redirect()
{
    remove_action('template_redirect', 'wp_redirect_admin_locations', 1000);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM