Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#51697 closed defect (bug) (reported-upstream)

[PHP8] Uncaught TypeError: array_flip():

Reported by: bph's profile bph Owned by:
Milestone: 5.6 Priority: normal
Severity: normal Version: 5.6
Component: Administration Keywords: php8
Focuses: Cc:

Description (last modified by SergeyBiryukov)

while using trunk on php8 I ran into this error, when i try to access wp-admin pages.

Fatal error: Uncaught TypeError: array_flip(): Argument #1 ($array) must be of type array, null given in public_html/wp-admin/includes/menu.php:280 

Stack trace: 
#0 public_html/wp-admin/includes/menu.php(280): array_flip(NULL) 
#1 public_html/wp-admin/menu.php(327): require_once('/home/customer/...') 
#2 public_html/wp-admin/admin.php(158): require('/home/customer/...') 
#3 public_html/wp-admin/index.php(10): require_once('/home/customer/...') 
#4 {main} thrown in public_html/wp-admin/includes/menu.php on line 280

Change History (8)

#1 @bph
4 years ago

  • Version set to trunk

#2 @hellofromTonya
4 years ago

  • Keywords php8 added

#3 @davidbaumwald
4 years ago

  • Component changed from General to Menus

#4 @SergeyBiryukov
4 years ago

  • Component changed from Menus to Administration
  • Description modified (diff)
  • Milestone changed from Awaiting Review to 5.6

#5 @jrf
4 years ago

Hi @bph, thanks for reporting this!

This is not necessarily an issue with WP Core, but more likely an issue with a plugin or theme doing it wrong.

Relevant code in wp-admin/includes/menu.php:

        $menu_order = array();
        foreach ( $menu as $menu_item ) {
                $menu_order[] = $menu_item[2];
        unset( $menu_item );
        $default_menu_order = $menu_order;

        $menu_order         = apply_filters( 'menu_order', $menu_order );
        $menu_order         = array_flip( $menu_order );
        $default_menu_order = array_flip( $default_menu_order );

$menu_order is an array and get pulled through the menu_order filter just before the array_flip(), so one of the functions filtering the variable will have broken things by either not returning anything or by returning null.

Now, it is tempting to add an (array) cast to fix this or to wrap the rest of the body of the condition in an if ( is_array( $menu_order ) ) {}, but that would only serve to hide the problem, not actually fix it.

This is a typical example of a filter which would really benefit from a type safe variant of apply_filters() as proposed in #51525.

For now, @bph, I'd like to ask you gather some more information either by using the Query Monitor or by using the Debug Bar Actions & Filter addon.

To figure out which plugin/theme/Core is causing the issue, we need to know what functions are hooked into the menu_order filter and where they are coming from (plugin/theme/Core) and if plugin/theme, preferably including information on which version of the plugin/theme you are using.

Once that information is available, all functions listed will need to be examined to figure out the culprit.

Last edited 4 years ago by jrf (previous) (diff)

#6 @noisysocks
4 years ago

Noting that I was seeing a warning to this effect while running PHP 7.4.12. GB26643 fixed it.

#7 @bph
4 years ago

Thank you @jrf for the detailed instructions on how to troubleshoot this further.
First, I will double-check on possible the fix @noisysocks indicates.

#8 @bph
4 years ago

  • Resolution set to reported-upstream
  • Status changed from new to closed

Tested with Gutenberg from master. PR 26643 fixed this instance. Closing it.

Note: See TracTickets for help on using tickets.