[英]Stop WooCommerce redirecting wp-login.php and wp-admin to account pages
With a plain-vanilla Wordpress install, a subscriber can access /wp-login.php, login and visit the dashboard. 使用普通的Wordpress安装,订阅者可以访问/wp-login.php,登录并访问仪表板。
I've found that after installing WooCommerce, if a subscriber is logged in and then tries to revisit the wp-admin or wp-login.php they are redirected to the my-account page set in the WooCommerce settings. 我发现在安装WooCommerce之后,如果订阅者登录然后尝试重新访问wp-admin或wp-login.php,则会将其重定向到WooCommerce设置中设置的my-account页面。
I wondered if there is anyway to remove this functionality as it isn't right for my site. 我想知道是否有任何方法要删除此功能,因为它不适合我的网站。
Any thoughts much appreciated. 任何想法都非常感激。
Solution 解
I have found a solution and posted it on this related question 我找到了一个解决方案并将其发布在这个相关问题上
You can use WooCommerce hooks to redirect users with different roles, see docs: https://docs.woocommerce.com/document/introduction-to-hooks-actions-and-filters/ 您可以使用WooCommerce挂钩重定向具有不同角色的用户,请参阅文档: https : //docs.woocommerce.com/document/introduction-to-hooks-actions-and-filters/
I Just Google 'woocommerce redirect subscribers' and your answer came in the first result :) 我只是谷歌'woocommerce重定向用户',你的答案来自第一个结果:)
So you can solve your website's issue using the Woocommerce hook filter woocommerce_login_redirect to redirect to a desired page based on the user role. 因此,您可以使用Woocommerce钩子过滤器woocommerce_login_redirect解决您的网站问题,根据用户角色重定向到所需的页面。
function wc_custom_user_redirect( $redirect, $user ) {
// Get the first of all the roles assigned to the user
$role = $user->roles[0];
$dashboard = admin_url();
$myaccount = get_permalink( wc_get_page_id( 'myaccount' ) );
if( $role == 'administrator' ) {
//Redirect administrators to the dashboard
$redirect = $dashboard;
} elseif ( $role == 'shop-manager' ) {
//Redirect shop managers to the dashboard
$redirect = $dashboard;
} elseif ( $role == 'editor' ) {
//Redirect editors to the dashboard
$redirect = $dashboard;
} elseif ( $role == 'author' ) {
//Redirect authors to the dashboard
$redirect = $dashboard;
} elseif ( $role == 'customer' || $role == 'subscriber' ) {
//Redirect customers and subscribers to the "My Account" page
$redirect = $myaccount;
} else {
//Redirect any other role to the previous visited page or, if not available, to the home
$redirect = wp_get_referer() ? wp_get_referer() : home_url();
}
return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'wc_custom_user_redirect', 10, 2 );
See source: https://gist.github.com/lmartins/28186383883d7c5ec644 参见来源: https : //gist.github.com/lmartins/28186383883d7c5ec644
Here a way to achieve that for the "subscriber" user role: 这里有一种方法来实现“订户”用户角色:
// Conditional function code for 'subscriber' User Role
function is_subscriber_user(){
if( current_user_can('subscriber') ) return true;
else return false;
}
// Redirect 'subscriber' User Role to the User edit prodile on WooCommerce's My Account
// So when he get looged or it register too
add_filter('template_redirect', 'wp_subscriber_my_account_redirect' );
function wp_subscriber_my_account_redirect() {
if( is_subscriber_user() && is_account_page() )
wp_redirect( get_edit_profile_url( get_current_user_id() ) );
}
// Prevent automatic woocommerce redirection for 'subscriber' User Role
add_filter( 'woocommerce_prevent_automatic_wizard_redirect', 'wc_subscriber_auto_redirect', 20, 1 );
function wc_subscriber_auto_redirect( $boolean ) {
if( is_subscriber_user() )
$prevent_access = true;
return $boolean;
}
// Allow 'subscriber' User Role to view the Dashboard
add_filter( 'woocommerce_prevent_admin_access', 'wc_subscriber_admin_access', 20, 1 );
function wc_subscriber_admin_access( $prevent_access ) {
if( is_subscriber_user() )
$prevent_access = false;
return $prevent_access;
}
// Show admin bar for 'subscriber' User Role
add_filter( 'show_admin_bar', 'wc_subscriber_show_admin_bar', 20, 1 );
function wc_subscriber_show_admin_bar( $show ) {
if ( is_subscriber_user() )
$show = true;
return $show;
}
Code goes in function.php file of the active child theme (or active theme). 代码位于活动子主题(或活动主题)的function.php文件中。
Tested and works. 经过测试和工作。
If you want the "subscriber" user to be redirected to the dashboard instead of the edit profile, you just have to replace
get_edit_profile_url()
function byget dashboard url()
… 如果您希望将“订户”用户重定向到仪表板而不是编辑配置文件,则只需通过get dashboard url()
替换get_edit_profile_url()
函数即可...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.