0

I would like to hide Pages and Comments from the editor.

I tried to override them with my custom types' menu_position but it doesn't work in WordPress 3.7.1 any more.

I found another way: removing the entries from admin menu by altering the global $menu variable on menu_order filter. Unfortunately it produces errors in menu.php: array_flip() gets passed a null.

I'm guessing that WordPress assumes that default values are still there.

Is there some way of filtering the menu entries when they are rendered? Even better: is there a way of setting the default post types public option to false?

class RemoveAdminMenuItems {

/**
 * List of values used for key identification
 * 
 * @var array
 */
public $remove_keys = array();

/**
 * List of values used for key identification. Identified keys will be removed.
 * 
 * @param array $remove_keys
 */
public function __construct($remove_keys)
{

    $this->remove_keys = $remove_keys;

    add_filter( 'custom_menu_order', '__return_true' );
    add_filter( 'menu_order', array($this, 'reconstruct_menu'));

}

/**
 * Method modifies the admin menu before it gets rendered.
 * 
 * @global array $menu
 */
public function reconstruct_menu() {

    global $menu;

    foreach ($menu as $menu_key => $menu_value) {
        foreach ($this->remove_keys as $remove_key) {
            $key = array_search($remove_key, $menu_value);
            if ($key) {
                unset($menu[$menu_key]);
            }        
        }
    }

}

 }
4
  • What do you mean by end user here? Nov 6, 2013 at 12:46
  • Obviously I meant the editor. :) Thx. Nov 6, 2013 at 13:39
  • Well, it wasn't obvious; else I wouldn't have asked. You could have meant users of a given user role - which could potentially lead to a viable solution, through modifying user capabilities. Nov 6, 2013 at 13:42
  • You are right. The answer was pretty obvious: User Capabilities. I prefer doing it by the book. Nov 6, 2013 at 16:18

1 Answer 1

0

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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