Home  >  Q&A  >  body text

HP Notice: PHP warning: Invalid argument provided for foreach() in plugin.php

I'm getting tons of errors in my error log. Does anyone know how to solve this problem?

They are all the same:

[Mon Jun 27 12:39:xx.518352 2022] [proxy_fcgi:error] [pid 4663:tid 139793920644864] [client 84.80.28.xx:52348] AH01071: Error occurred "PHP message: PHP warning: Invalid " is the parameter provided for foreach() in /home/685947.example.com/public_html/wp-admin/includes/plugin.php on line 1779, reference address: https://example.com/mijn-account/

This is the code (default WordPress file):

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

P粉561749334P粉561749334334 days ago552

reply all(2)I'll reply

  • P粉553428780

    P粉5534287802023-12-14 10:45:50

    The value received by the variable ($menu) may not be an array.

    It would be fun to add validation, for example:

    function remove_menu_page( $menu_slug ) {
        global $menu;
        
        if (!is_array($menu)) return false;
    
        foreach ( $menu as $i => $item ) {
            if ( $menu_slug === $item[2] ) {
                unset( $menu[ $i ] );
                return $item;
            }
        }
    
        return false;
    }

    reply
    0
  • Jenson.Wang

    Jenson.Wang2023-12-15 11:36:25

    Variable ($menu) may be an empty array

    function remove_menu_page( $menu_slug ) {    
        global $menu;    
        if(!empty($menu)){ 
            foreach ( $menu as $i => $item ) {        
                if ( $menu_slug === $item[2] ) {            
                    unset( $menu[ $i ] );            
                    return $item;        
                }   
            }  
        }  
      return false;
    }

    reply
    0
  • Cancelreply