WordPress remove_menu_page() function throws an error

I am trying to remove a few top-level menus on the WordPress admin panel. Oddly enough, I get an error message from the plugin.php file, where the function is declared, saying:

Invalid argument supplied for foreach() in C:wampwwwwordpress-alutwp-adminincludesplugin.php on line 1261

Read More

I went to the file and found the following code:

function remove_menu_page( $menu_slug ) {
    global $menu;

    foreach ( $menu as $i => $item ) { // **this is line 1261**
        if ( $menu_slug == $item[2] ) {
            unset( $menu[$i] );
            return $item;
        }
    }

    return false;
}

It is important to note that when I use remove_submenu_page(), which is the next function in plugin.php, I get no such error.
My function, located in functions.php:

add_action( 'admin_init', 'mf_remove_menu_pages' );
function mf_remove_menu_pages() {
    remove_menu_page('link-manager.php');
    remove_menu_page('index.php');
    remove_menu_page('users.php');
    remove_menu_page('upload.php');
    remove_menu_page('tools.php');
    remove_menu_page('edit.php');
    remove_menu_page('edit-comments.php');
    remove_menu_page('post-new.php');
    remove_submenu_page('themes.php','themes.php');
    remove_submenu_page('themes.php','theme-editor.php');
    remove_submenu_page('themes.php','widgets.php');
}; 

Related posts

Leave a Reply

2 comments

  1. You’re calling mf_remove_menu_pages() before $menu is actually set up, or it seems in cases when $menu is never set up. You need to wait for the admin_menu hook in order to actually call remove_menu_page(). If you’re doing it on admin_init, then you’re using the wrong hook, and that’s why it’s borking on AJAX requests

    So you need to change your hook. Please try with this:

    add_action( 'admin_menu', 'mf_remove_menu_pages' );
    function mf_remove_menu_pages() {
       remove_menu_page('link-manager.php');
       remove_menu_page('index.php');
       remove_menu_page('users.php');
       remove_menu_page('upload.php');
       remove_menu_page('tools.php');
       remove_menu_page('edit.php');
       remove_menu_page('edit-comments.php');
       remove_menu_page('post-new.php');
       remove_submenu_page('themes.php','themes.php');
       remove_submenu_page('themes.php','theme-editor.php');
       remove_submenu_page('themes.php','widgets.php');
    }
    

    Hope this helps 🙂

  2. In newer versions of WordPress, to remove some subpages like theme-editor.php (or subpages introduced by the Jetpack plugin) you have to bind to the admin_menu hook with a very high priority.

    E.g. to remove theme-editor.php you need to set a priority of about 120. For Jetpack pages, you need somthing around 2000.

    add_action( 'admin_menu', 'mf_remove_menu_pages', 120 );
    function mf_remove_menu_pages() {
       remove_menu_page('link-manager.php');
       remove_menu_page('index.php');
       remove_menu_page('users.php');
       remove_menu_page('upload.php');
       remove_menu_page('tools.php');
       remove_menu_page('edit.php');
       remove_menu_page('edit-comments.php');
       remove_menu_page('post-new.php');
       remove_submenu_page('themes.php','themes.php');
       remove_submenu_page('themes.php','theme-editor.php');
       remove_submenu_page('themes.php','widgets.php');
    }
    

    This is also stated in the WordPress Codex and at the WordPress Support Forums.