[resolved] Using remove_menu_page causes error throughout other admin sections (5 posts)

  1. Gabe Shackle
    Posted 4 years ago #

    As soon as I add a call to remove_menu_page in my functions.php I get several errors in the Pages and Menus section of the admin. The error is always:

    Warning: Invalid argument supplied for foreach() in ~sitepath~\wp-admin\includes\plugin.php on line 1261

    It doesn't appear to matter what menu I try to remove, if any calls to that function are made it causes the error to occur. A couple specific places it occurs is when using Quick Update on a post/page or when adding items to a Menu.

    I'm calling the remove_menu_page from the 'admin_init' hook.

  2. Gabe Shackle
    Posted 4 years ago #

    Sorry, just found the bug post for it. I was using the wrong hook. The correct hook is 'admin_menu'

  3. timfarley
    Posted 4 years ago #

    Thanks for the post - helped me solve that annoying issue as well. Just to clarify for anyone who stumbles onto this post: If you have a function to hide some of the menu items on the left side admin panel, like this:

    function mytheme_remove_menu_pages() {
    add_action( 'admin_init', 'mytheme_remove_menu_pages' );

    Change the last line to
    add_action( 'admin_menu', 'mytheme_remove_menu_pages' );

  4. Parrudex
    Posted 4 years ago #

    Great solution timfarley.. thanks!

  5. RevConcept
    Posted 4 years ago #

    Thanks for posting this! I thought I was going to have to troubleshoot forever, but I changed my code to the following and it works!

    What was Causing the error:
    I was changing post status to "draft" via quick edit.

    This is the error I had:
    Warning: Invalid argument supplied for foreach() in (blah blah) ...plugin.php on line 1261

    Here is the code I changed like @timfarley posted above:

    add_action( 'admin_menu', 'nz_remove_menu_pages' );
    function nz_remove_menu_pages() {
      // If the user does not have access to publish posts
      if(!current_user_can('activate_plugins')) {
        // Remove the "Tools" menu

    First line had previously been:
    add_action( 'admin_init', 'nz_remove_menu_pages' );

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.