With a plain-vanilla Wordpress install, a subscriber can access /wp-login.php, login and visit the dashboard.
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.
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/
I Just Google 'woocommerce redirect subscribers' and your answer came in the first result :)
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.
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
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).
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()
…
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.