简体   繁体   中英

How to hide “WooCommerce” from menu for non admin?

I tried the following but it didn't work

add_action( 'admin_menu', 'wpse28782_remove_menu_items' );
function remove_menus(){
remove_menu_page( 'edit.php?post_type=shop_order' );       
}

add_action( 'admin_menu', 'remove_menus' );

How can I hide it?

One option is to use the admin menu editor plugin which will let you modify your admin menu based on a users permissions.

https://en-au.wordpress.org/plugins/admin-menu-editor/

Alternatively you can use a solution based on the users capabilities, this would target anyone that doesn't have admin privileges:

add_action( 'admin_menu', 'no_woo' );

function no_woo() {
    if ( current_user_can('manage_options') == false ) {
        remove_menu_page( 'woocommerce' );
    }
}

Case 1: Modify code to achive this, (Universal Solution)

You can use the following to debug:

add_action( 'admin_init', 'wodebug_admin_menu' );

function wodebug_admin_menu() {

    echo '<pre>' . print_r( $GLOBALS[ 'menu' ], TRUE) . '</pre>';
}

This gives (for my setup) the following for the Contact Form 7 plugin menu page:

[27] => Array
        (
            [0] => Formular
            [1] => wpcf7_read_contact_forms
            [2] => wpcf7
            [3] => Contact Form 7
            [4] => menu-top menu-icon-generic toplevel_page_wpcf7 menu-top-last
            [5] => toplevel_page_wpcf7
            [6] => none
        )

get the key and apply in your case.

add_action( 'admin_init', 'wpse_136058_remove_menu_pages' );

function wpse_136058_remove_menu_pages() {

    remove_menu_page( 'edit.php?post_type=acf' );
    remove_menu_page( 'wpcf7' ); // Key place in this
}

Case 2: Use a Plugin.

http://wordpress.org/plugins/adminimize/

This week I requested WooCommerce to change their setup, so this is better to manage. The idea needs votes, so please add your vote and hope we don't need to code such anymore: https:ideas.woocommerce.com . Please give it the maximum 3 points, thanks!

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM