编辑子主题功能,它位于:
C: \\xampp\\htdocs\\your website\\wp content\\themes\\your theme\\functions。php
然后在代码的底部,insert these 3 functions.
FUNCTION #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 );
}
FUNCTION #2就我而言,我想
redirect
用户进入
Sign in/Registration
如果他们想访问
wishlist
或者想进入
checkout
页面,成功登录后,他们将重定向回上一页。
/**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
}
}
FUNCTION #3最后一件事是在成功登录后将重定向处理回上一页。
出于某种原因,如果您使用的是默认登录页面wp-login.php
而且不是自定义登录页面,成功登录后重定向可以在不使用以下代码的情况下工作,我仍然在搜索有关它的解释,因为我刚刚接触WoodPress,我认为这与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);
}
我不确定这在安全性和bug问题上是否是正确的方法,我希望有人会指出正确的方法,如果有,如果我找到更好的方法,我会编辑这个。