Change WordPress user roles and capabilities › Forums › Restrict or Permit access inside WordPress – how to › WooCommerce – My account -page restrict access to tabs › Reply To: WooCommerce – My account -page restrict access to tabs
30/09/2016 at 14:53
#2816
Vladimir
Keymaster
Hi,
Try to install this code as a “must use” plugin. Replace ‘subscriber’ role inside to you own role ID, e.g. ‘candidate’:
add_filter('woocommerce_account_menu_items', 'filter_wc_my_account_menu');
add_action('template_redirect', 'redirect_for_blocked_wc_pages');
function filter_wc_my_account_menu($items) {
if (!current_user_can('subscriber')) {
return $items;
}
if (isset($items['dashboard'])) {
unset($items['dashboard']);
}
if (isset($items['orders'])) {
unset($items['orders']);
}
if (isset($items['downloads'])) {
unset($items['downloads']);
}
if (isset($items['edit-address'])) {
unset($items['edit-address']);
}
if (isset($items['payment-methods'])) {
unset($items['payment-methods']);
}
return $items;
}
function check_end_point_url($end_point, $current_url) {
$blocked_url = wc_get_endpoint_url($end_point);
if ($current_url==$blocked_url) {
$my_account_url = wc_get_endpoint_url('woo-account-page');
wp_redirect($my_account_url);
die;
}
}
function redirect_for_blocked_wc_pages() {
global $wp;
if (!current_user_can('subscriber')) {
return;
}
$current_url = trailingslashit(home_url($wp->request));
$blocked_end_points = array('dashboard', 'orders', 'downloads', 'edit-address', 'payment-methods');
foreach($blocked_end_points as $bep) {
check_end_point_url($bep, $current_url);
}
}