【问题标题】:Changing auth_redirect() page更改 auth_redirect() 页面
【发布时间】:2020-06-23 04:57:36
【问题描述】:

我知道auth_redirect() 检查用户是否登录,如果没有,它会将他们重定向到登录页面,然后在成功时返回上一页。我需要在我的网站上使用该功能,并且我拥有它。

例如,我希望其中一个页面只能由登录用户访问,如果他们试图访问该页面,那么他们需要先登录,然后在成功返回该页面。我的function.php 上有以下代码。

if ( is_page('user_only') && ! is_user_logged_in() ) {
    auth_redirect();
}

问题是我有一个自定义登录/注册页面,而不是默认的 WordPress 登录页面,我希望 auth_redirect(); 使用我的自定义登录/注册页面。

auth_redirect(); 正在使用wp-login.php,我想使用我的自定义页面account/index.php。 这可以做到吗?我知道wp_redirect( url, );,但我认为我不需要它,因为它的目的仅用于重定向而不是用于身份验证。

【问题讨论】:

  • 如果你检查函数的代码,你会看到它使用wp_login_url 来确定要重定向到的登录 URL,如果你再看一遍,你会看到有一个过滤器命名为login_url,你可以挂钩,修改它。
  • @CBroe - 我检查了general_template.php 然后在wp_login_url() 功能上,我已经将wp-login.php 更改为自定义登录页面并且它可以工作。不幸的是,它并没有重定向回以前的地址,它只是在帐户页面上并登录。
  • wp_login_url 允许您指定成功登录后系统将用户重定向到的 URL,login_url 过滤器也将其传入。您所要做的就是将其添加到您的登录 URL 作为查询字符串参数,名为 redirect_to
  • @CBroe - 你能用代码详细说明吗?在使用我的自定义登录页面更改general_template.phpwp_login_url() 上的wp-login.php 函数后,当我转到只有登录用户可以访问的页面时,我会得到这个url http://localhost:5597/website/custom-login/?redirect_to=http%3A%2F%2Flocalhost%3A5597%2Fwebsite%2FuserOnly%2F&reauth=1。如您所见,根据 url,当来自custom-login 的身份验证成功时,它应该重定向回userOnly 页面。但它不起作用,成功登录后,它只是在custom-login page的个人资料上。
  • 那么,当您说您有一个“自定义登录/注册页面”时,这到底是什么意思?您是否只是更改了布局,但它仍在使用默认的 WP 功能?如果没有,并且您在那里自己处理登录或以任何其他主要方式进行干预,它可能不再按预期工作。

标签: php wordpress authentication woocommerce wordpress-theming


【解决方案1】:

编辑子主题功能,位于:

C:\xampp\htdocs\your-website\wp-content\themes\your-theme\functions.php

然后在代码的底部,插入这3个函数

功能 #1 将默认登录 URL(wp-login.php)更改为您的自定义页面。例如https://localhost/my-website/my-account/

/**Function to change the default `wp-login.php` with your custom login page **/
add_filter( 'login_url', 'new_login_page', 10, 3 );
function new_login_page( $login_url, $redirect, $force_reauth ) {
    $login_page = home_url( '/my-account/' ); //use the slug of your custom login page.
    return add_query_arg( 'redirect_to', $redirect, $login_page );
}

功能 #2 就我而言,如果用户想访问wishlist 或想继续进入checkout 页面,我想将redirect 用户转到Sign in/Registration 页面, 成功登录后,他们将重定向回上一页。

/**Function to redirect into `logged-in/Registration` page if not logged-in**/
add_action('template_redirect', 'redirect_if_not_logged_in');
function redirect_if_not_logged_in() {
    if (!is_user_logged_in() && (is_page('wishlist') || is_page('checkout'))) {
        auth_redirect(); //redirect into my custom login page
    }
}

功能#3 最后就是在成功登录后处理重定向回到上一页。

由于某种原因,如果您使用的是默认登录页面 wp-login.php 而不是自定义登录页面,则重定向在成功登录后无需使用以下代码即可工作,我仍在寻找解释因为我是 WordPress 的新手,所以我认为这与 Woocommerce 的自定义登录页面有关。 否则,您可以在成功登录后使用以下代码重定向回上一页。

//function to create the redirection url
function redirect_link($redirect){
    //extract the redirection url, in my case the url with rederiction is https://my-website/my-account/?redirect_to=https://my-website/the-slug/ then I need to get the
    //https://my-website/the-slug by using the `strstr` and `substr` function of php.
    $redirect = substr(strstr($redirect, '='), 1); 

    //decode the url back to normal using the urldecode() function, otherwise the url_to_postid() won't work and will give you a different post id.
    $redirect = urldecode($redirect);

    //get the id of page that we weanted to redirect to using url_to_postid() function of wordpress.
    $redirect_page_id = url_to_postid( $redirect );

    //get the post using the id of the page
    $post = get_post($redirect_page_id); 

    //convert the id back into its original slug
    $slug = $post->post_name;

    if(!isset($slug) || trim($slug) === ''){ //if slug is empty or if doesn't exist redirect back to shop
        return get_permalink(get_page_by_path('shop'));
    }
    //re-create the url using get_permalink() and get_page_by_path() function.
    return get_permalink(get_page_by_path($slug));
}

/**Function to redirect back to previous page after succesfful logged-in**/
add_filter( 'woocommerce_login_redirect', 'redirect_back_after_logged_in');
function redirect_back_after_logged_in($redirect) {
    return redirect_link($redirect);
}

/**Function to redirect back to previous page after succesfful registration**/
add_filter( 'woocommerce_registration_redirect', 'cs_redirect_after_registration');
function cs_redirect_after_registration( $redirect ){
    return redirect_link($redirect);
}

我不确定在安全性和错误问题方面这是否是正确的方法,我希望有人指出正确的方法,如果有更好的方法,我会编辑它。

【讨论】:

  • 谢谢你先生,你救了我的一天:)
猜你喜欢
  • 2011-05-30
  • 2021-10-15
  • 2013-09-21
  • 1970-01-01
  • 2018-08-30
  • 1970-01-01
  • 2012-06-02
  • 2011-04-27
  • 2022-01-24
相关资源
最近更新 更多