0
add_action( 'admin_init', 'my_remove_menu_pages' );
function my_remove_menu_pages() {

    global $user_ID;

    if ( current_user_can( 'contributor' ) ) {
remove_menu_page('tools.php'); // Tools
remove_menu_page('edit-comments.php'); // Comments
 remove_menu_page('edit.php'); // posts? 
 remove_menu_page( 'profile.php'); // profile
 remove_menu_page('index.php');  
    remove_menu_page( 'post-new' ); 
    }
    remove_menu_page('users.php');
    remove_menu_page('tools.php');
    remove_menu_page('themes.php'); //appearance
    remove_menu_page( 'edit.php?post_type=acf-field-group' ); // advanced custom fields
    remove_menu_page( 'edit-comments.php' );
}

I don't understand why it won't load the media library when I click on 'add media', whilst the above code is in functions.php. Please can someone explain?

0

The correct hook to add/remove menus in admin area is admin_menu. Also, be aware that current_user_can is intented to check user capabilities, not user roles. See current_user_can documentation and notes.

add_action( 'admin_menu', 'my_remove_menu_pages' );
function my_remove_menu_pages() {

    $user = wp_get_current_user();

    if( ! empty( $user ) && in_array( "contributor", (array) $user->roles ) ) {

        remove_menu_page('tools.php'); // Tools
        remove_menu_page('edit-comments.php'); // Comments
        remove_menu_page('edit.php'); // posts? 
        remove_menu_page( 'profile.php'); // profile
        remove_menu_page('index.php');  
        remove_menu_page( 'post-new' );

    }

    remove_menu_page('users.php');
    remove_menu_page('tools.php');
    remove_menu_page('themes.php'); //appearance
    remove_menu_page( 'edit-comments.php' );

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.