How to remove/hide elements from the admin menu?

If you just want to hide the menu items, you can do it like shown below. Don’t forget about the entries inside the admin bar.

Code:

//hide in admin menu
add_action( 'admin_menu', 'wpse121406_hide_pages_comments_m' );
function wpse121406_hide_pages_comments_m() {
    remove_menu_page('edit.php?post_type=page');
    remove_menu_page( 'edit-comments.php' );
}

//hide in admin bar
add_action( 'wp_before_admin_bar_render', 'wpse121406_hide_pages_comments_b' );
function wpse121406_hide_pages_comments_b() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('new-page');
    $wp_admin_bar->remove_menu('comments');
}

But you have to keep in mind that this doesn’t disable/restrict the functionality. The edit pages and the comments page can still be reached by typing in the address into the location bar. If you want to restrict access by user type you have to go with Roles and Capabilities.


Update:

Regarding your second question, on how to alter the built in post types properties. You can achieve this by going at it like shown below. You can use the registered_post_type or the init hook and can either use get_post_type_object() or the global $wp_post_types.

Code:

add_action('init','wpse121406_alter_post_type_object');
function wpse121406_alter_post_type_object() {
  $object = get_post_type_object('page');
  $object->public = false;
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)