个性化阅读
专注于IT技术分析

更改auth_redirect()页面

点击下载

我了解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, );但我认为我不需要这样做, 因为它的目的只是用于重定向, 而不是用于身份验证。


#1


编辑子主题功能, 它位于:

C:\ xampp \ htdocs \你的网站\ wp-content \ themes \你的主题\ functions.php

然后在代码底部, 插入这三个函数。

功能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就我而言, 如果用户想要访问愿望清单或想进入结帐页面, 我想将其重定向到”登录/注册”页面, 成功登录后, 他们将重定向回上一页。

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

我不确定这是否是解决安全性和错误问题的正确方法, 希望有人指出正确的方法, 如果发现更好的方法, 我会对其进行编辑。

赞(0)
未经允许不得转载:srcmini » 更改auth_redirect()页面

评论 抢沙发

评论前必须登录!