How do I fix this error: Warning: invalid argument supplied for foreach()?

I recently moved a wordpress install from my local instance to a dev server. For the move, I installed a clean version of wordpress 3.4, moved exact duplicates of the file structre, and used the import / export feature to bring the posts in.

I then went in and set the necessary widgets, and settings. I’ve done this 100 times, and never had this problem. Here is the error:

Read More

Warning: Invalid argument supplied for foreach() in /home/content/46/9411746/html/dev/wp-admin/includes/plugin.php on line 1285

It only appears in the admin menu. I see it when I try to add a widget, change a setting, work with menus, or update meta data for a post. It pops up all over the admin menu.

Here is the function triggering the error in includes/plugin.php

function remove_menu_page( $menu_slug ) {
global $menu;

foreach ( $menu as $i => $item ) {
    if ( $menu_slug == $item[2] ) {
        unset( $menu[$i] );
        return $item;
    }
}

return false;
}

I’m using appearance > menus and have two registered here in functions.php here:

add_action( 'init', 'register_my_menus' );

function register_my_menus() {
register_nav_menus(
    array(
      'header-nav' => __( 'Main Header Navigation', 'kyosay' ),
      'footer-nav' => __( 'Footer Navigation', 'kyosay' )
    )
);
}

I am doing some other customization of the admin panel, reordering some menu items and eliminating others that my clients don’t need. I can include that code if you think it’s relevant.

Since this issue is triggered in core, I’m at a loss as to how to fix it. NOTE: The issue is not happening on my local build. Thoughts?

Edit: added remove_menu_items code from functions.php for reference

function remove_menu_items() {
remove_menu_page('link-manager.php'); // Links
remove_menu_page('edit-comments.php'); // Comments
}
add_action( 'admin_init', 'remove_menu_items' );

Update:
I have eliminated functions.php as the source of this issue. It seems to be triggered on Ajax events (dragging a new widget to a sidebar, updating a meta-box, etc. I’m going to uninstall and reinstall and see if it’s still happening. Could this have something to do with the web host (godaddy) ? It’s not showing up on my local build at all.

Related posts

Leave a Reply

2 comments

  1. This usually happens when you are trying to do admin navigation stuff and you aren’t hooked into admin_menu. If you hook in before that, $menu hasn’t been created yet.

    add_action('admin_menu', 'my_plugin_add_menu');
    
    function my_plugin_add_menu(){
        $ptype = 'my_post_type';
        $ptype_obj = get_post_type_object( $ptype );
        add_submenu_page( 'my-menu-item', $ptype_obj->labels->name, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, "edit.php?post_type=$ptype" );
    }
    
  2. I had a similar problem with wordpress, just added an if statement to check the argument as follows

    if(is_array($thearray)){
        //use $thearray here
    }
    

    and the warning was gone!!