更改 auth_redirect() 页面

Changing auth_redirect() page

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

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

上有以下代码
if ( is_page('user_only') && ! is_user_logged_in() ) {
    auth_redirect();
}

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

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

编辑子主题功能,位于:

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);
}

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