To do that use the following function:
add_action( 'admin_menu', 'custom_menu_page_removing' );
function custom_menu_page_removing() {
global $pagenow;
if ( !current_user_can( 'administrator' ) ) {
$menu = array(
'edit.php',
'edit-comments.php'
);
foreach ( $menu as $main ) {
remove_menu_page( $main );
}
if ( in_array( $pagenow, $menu, true ) && ( !isset( $_GET['post_type'] ) || isset( $_GET['post_type'] ) && $_GET['post_type'] == 'post' ) ) {
wp_die( 'Access denied...' );
exit;
}
}
}
This will execute the function only if the user is not an admin. Where you see edit.php
and edit-comments.php
, you can replace and add more menu slug that you want to hide. Make sure you add a ,
after each one except for the last one.