当我们添加一个User为shop manager角色的时候,这个user是可以看到WooCommerce, Analytics菜单的,但有时候不需要看到这两个菜单,只需要看到Products菜单就可以了。如何屏蔽掉WooCommerce与Analytics菜单呢?

使用ShortCode的方式可以屏蔽掉。

代码:

代码放到后台——Appearance——Theme File Editor——Functions.php 文件末尾


function ensky_remove_dashboard_widgets() {
    if ( current_user_can( 'shop_manager' ) ) {
        // remove WooCommerce Dashboard Status
        remove_meta_box( 'woocommerce_dashboard_status', 'dashboard', 'normal' );
    }
}
add_action( 'wp_user_dashboard_setup', 'ensky_remove_dashboard_widgets', 20 );
add_action( 'wp_dashboard_setup', 'ensky_remove_dashboard_widgets', 20 );

function ensky_disable_dashboard_woo_analytics()
{
    // If the current user is not an admin
    if (!current_user_can('manage_options') ) {
        remove_menu_page( 'woocommerce' ); // WooCommerce admin menu slug
		remove_menu_page( 'wc-admin&path=/analytics/overview');
    }
}
add_action( 'admin_menu', 'ensky_disable_dashboard_woo_analytics',110 );
上述代码是针对非管理员角色(current user is not an admin or do not have administrative access)的时候,屏蔽掉WooCommerce与Analytics菜单

没有屏蔽掉的时候:

屏蔽掉WooCommerce与Analytics菜单之后:

如果想针对单一角色屏蔽某个菜单,使用的判断条件是:

if ( current_user_can( ‘shop_manager’ ) )

如果想屏蔽WooCommerce子菜单,可以使用的代码如下:

    //remove dashboard /WooCommerce submenu, except 'order' and 'customers'

    remove_submenu_page( 'woocommerce', 'wc-addons' );
    remove_submenu_page( 'woocommerce', 'wc-status' );
    remove_submenu_page( 'woocommerce', 'wc-settings' );
    remove_submenu_page( 'woocommerce', 'wc-reports' );
    remove_submenu_page( 'woocommerce', 'wc-admin' );
}

完整的可以是:

//remove the analytics and woocommerce submenu
function ensky_remove_wc_reports() {
if ( current_user_can( 'shop_manager' ) ) {

    //remove dashboard /Analytics
    remove_menu_page( 'wc-admin&path=/analytics/overview' );

    //remove dashboard /WooCommerce submenu, except 'order' and 'customers'

    remove_submenu_page( 'woocommerce', 'wc-addons' );
    remove_submenu_page( 'woocommerce', 'wc-status' );
    remove_submenu_page( 'woocommerce', 'wc-settings' );
    remove_submenu_page( 'woocommerce', 'wc-reports' );
    remove_submenu_page( 'woocommerce', 'wc-admin' );
}
}