简体   繁体   English

在WordPress自定义登录页面登录后重定向回帖子

[英]Redirect back to the post after login in the WordPress custom login page

In my WordPress v5.8.2, I have a custom login page http://www.....com/login .在我的 WordPress v5.8.2 中,我有一个自定义登录页面http://www.....com/login

Site allows only logged in users to post comments and below redirect URL link is published on the comments section for user to login:网站只允许登录用户发表评论,下面重定向 URL 链接发布在评论部分供用户登录:

http://www......com/login/?redirect_to=http://www......com/post-name

I am trying to redirect back to the post after login with below function:我正在尝试使用以下 function 登录后重定向回帖子:

function redirect_to_posts_after_login($redirect_to, $requested_redirect_to) {
    $redirect_to_post = $_GET['redirect_to'];
    if (!$redirect_to_post === "") {
        return $redirect_to_post;
    } else {
        return $requested_redirect_to;
    }
}

add_filter('login_redirect', 'redirect_to_posts_after_login', 10, 3);

However, it still redirects to wp-admin after the login.但是,它仍然会在登录后重定向到wp-admin How can I make the redirect back to the post?如何使重定向回到帖子?

Since you are trying to redirect the user, to your desired custom URL after login.由于您正在尝试将用户重定向到登录后所需的自定义 URL。 Then you should insert your custom URL here:-然后你应该在这里插入你的自定义 URL: -

    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
            //check for admins
            if ( in_array( 'administrator', $user->roles ) ) {
                // redirect them to the default place
                return $redirect_to;
            } else {
                //you shud concatinate your custom url here
                return home_url();
            }
        } else {
            return $redirect_to;
        }

$redirect_to variable contains the URL to the wp-admin. $redirect_to 变量包含到 wp-admin 的 URL。 But as a user apart from admin.但是作为管理员以外的用户。 You can insert the custom URL beside the home_url() function.(Concatinate your custom url in the form hard coded string)您可以在 home_url() function 旁边插入自定义 URL。(以硬编码字符串的形式连接您的自定义 url)

With below changes, now I am able to return the user to the post after login.通过以下更改,现在我可以在登录后将用户返回到帖子。

Login URL at each post remained same as below with redirect_to :在每个帖子中登录 URL 与redirect_to保持如下相同:

http://www......com/login/?redirect_to=http://www......com/post-name

In my custom login page template login I am capturing the redirect_to value as a variable:在我的自定义登录页面模板login中,我将redirect_to值作为变量捕获:

$redirect_back_to = $_GET['redirect_to'];

And then in the same custom login page I have this hidden input field that take the redirect_to as input value:然后在同一个自定义登录页面中,我有这个隐藏的输入字段,它将redirect_to作为输入值:

  <input type="hidden" value="<?php echo $redirect_back_to; ?>" name="redirect_to_post_url">

In the below function I am capturing the post referrer URL from the above input and checks if there is a referrer ( redirect_to ) it will redirect to the post else to wp-admin :在下面的 function 中,我正在从上述输入中捕获帖子引荐来源 URL 并检查是否存在引荐来源( redirect_to ),它将重定向到帖子,否则将重定向到wp-admin

function redirect_to_posts_after_login($redirect_to) {
    $redirect_to_post = filter_input(INPUT_POST, 'redirect_to_post_url');

    if (!$redirect_to_post === "") {
        wp_redirect(esc_url($redirect_to_post));
    } else {
        return $redirect_to;
    }
}

add_filter('login_redirect', 'redirect_to_posts_after_login', 10, 1);

!$redirect_to_post === "" will always return false ! !$redirect_to_post === ""将始终返回 false !

can you try with this code?你可以试试这个代码吗?

function redirect_to_posts_after_login($redirect_to, $requested_redirect_to) {
    $redirect_to_post = $_GET['redirect_to'];
    if ($redirect_to_post !== "") {
        return $redirect_to_post;
    } else {
        return $requested_redirect_to;
    }
}

add_filter('login_redirect', 'redirect_to_posts_after_login', 10, 3);

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

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