Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've got the following code, which cleans up a lot of stuff that is not going to be used within the admin area:

add_action( 'admin_menu', 'my_remove_menu_pages' );

function my_remove_menu_pages() {
  remove_menu_page( 'edit.php' );                   //Posts
  remove_menu_page( 'upload.php' );                 //Media
  remove_menu_page( 'edit-comments.php' );          //Comments
  remove_menu_page( 'themes.php' );                 //Appearance
  remove_menu_page( 'users.php' );                  //Users
  remove_menu_page( 'tools.php' );                  //Tools
  remove_menu_page( 'options-general.php' );        //Settings

};

However, there are two menu items that have been inserted from plugins.

When I hover over each menu item, it says the links are:

/wp-admin/edit.php?post_type=acf
/wp-admin/admin.php?page=wpcf7

Is there a way to hide these menu pages, too?

share|improve this question
    
May be Stupid to ask, but why hack the admin menu, when you could deactivate the plugins in question? –  E Carter Young May 23 at 21:27

3 Answers 3

You probably need to set the add_action priority to a higher number and target those two new links specifically like so:

add_action( 'admin_menu', 'my_remove_menu_pages', 999 );

function my_remove_menu_pages() {
  remove_menu_page( 'edit.php' );                   //Posts
  remove_menu_page( 'upload.php' );                 //Media
  remove_menu_page( 'edit-comments.php' );          //Comments
  remove_menu_page( 'themes.php' );                 //Appearance
  remove_menu_page( 'users.php' );                  //Users
  remove_menu_page( 'tools.php' );                  //Tools
  remove_menu_page( 'options-general.php' );        //Settings
  remove_menu_page( 'edit.php?post_type=acf' );
  remove_menu_page( 'wpcf7' );
};

Advanced Custom Fields also provides a help document on how to do this for their menu here:

http://www.advancedcustomfields.com/resources/how-to/how-to-hide-acf-menu-from-clients/

Contact Form 7 also has it's own way of restricting the visibility of the menu:

http://contactform7.com/restricting-access-to-the-administration-panel/

share|improve this answer

You need to use the right hooks (which are not always the same as the URL/slug), and it doesn't hurt do use a hook that runs later (e.g., admin_init):

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');
} // function wpse_136058_remove_menu_pages

You can use the following to debug:

add_action('admin_init', 'wpse_136058_debug_admin_menu');
function wpse_136058_debug_admin_menu() {
    echo '<pre>'.print_r($GLOBALS['menu'], true).'</pre>';
} // function wpse_136058_debug_admin_menu

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
        )

And the array element with key 2 is what you are looking for: wpcf7

share|improve this answer

Check out this link for another approach -- it might work for you better.

share|improve this answer
    
Lone link is considered a poor answer (see help > deleted answers) since it is meaningless by itself and target resource is not guaranteed to be alive in the future. Please try to include at least summary of information you are linking to. –  Rarst Feb 26 at 18:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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