Make WordPress Core

Opened 4 years ago

Last modified 29 hours ago

#56254 new defect (bug)

PHP Warning: Undefined array key 1 wp-admin\menu-header.php on line 202

Reported by: pras.88in Owned by:
Priority: normal Milestone: Awaiting Review
Component: Administration Version:
Severity: normal Keywords: php81 has-patch has-unit-tests
Cc: Focuses:

Description

i get the above warning when i use php version 8.1.6 in iis server,for php7.427 the code works , am using wordpress 6.0.1 released on jul 12 2022, am unable to fix it , kindly support , i get this error after succesfull login as admimistrator in plugin.php page , and other admin pages.

Change History (9)

#1 @sabernhardt
4 years ago

  • Component General → Administration
  • Keywords php81 added

Hi and welcome to Trac!

If you want site support, the support forums would be a better place to ask:
​https://wordpress.org/support/forums/

However, I'll leave this ticket open for now. The undefined key is the capability for a sub-menu item in the side admin menu, and maybe the condition needs to check whether the key is set/non-empty before using current_user_can().

Version 0, edited 4 years ago by sabernhardt (next)

This ticket was mentioned in ​PR #3140 on ​WordPress/wordpress-develop by ​Tabrisrp.


4 years ago
#2

  • Keywords has-patch added

Check for non-empty array key 1 before using it with current_user_can() to avoid PHP warning

Trac ticket: https://core.trac.wordpress.org/ticket/56254

#3 @desrosj
4 years ago

  • Keywords needs-unit-tests added
  • Version trunk

Adding needs-unit-tests since some were requested in the PR.

#4 @hellofromTonya
3 years ago

The code in the foreach was introduced in [8691] over 15 years ago:

// 0 = menu_title, 1 = capability, 2 = menu_slug, 3 = page_title, 4 = classes.
foreach ( $submenu_items as $sub_key => $sub_item ) {
   if ( ! current_user_can( $sub_item[1] ) ) {

I wonder: Why would the capability element be missing in the $sub_item?

Adding defensive code before attempting to access it is good, but can hide a potential problem. I'm hesitant to move forward with ​the patch without first understanding the root cause.

Seems this code is expecting the format identified which includes the capability element being at index 1.

I'm wondering ..

  • Should sub items always be an array with element 1 as the capability?
  • Are there checks or set up of the array structure where the menu is created, i.e. executing before the above code runs?
  • If yes, then this is likely a plugin or theme doing it wrong?

The answers will take investigation.

@pras88in more information is needed for contributors like me to investigate:

  • Can you share the steps of how to reproduce the issue?
  • Can you share the backtrace call stack given with the PHP error? Copy and paste the full error message here.
  • After deactivating all of the plugins, does the issue go away? If yes, then one or more of the plugins is the issue. You can reactivate each plugin one-by-one to discover which is causing the problem. Then report the issue to the plugin author.

#5 @hellofromTonya
3 years ago

Is this issue an incompatibility with PHP 8.1?

I don't think there's enough information to know if this is an issue in Core. Thus, I'm not adding the php-compatibility focus to list it in exclusions of PHP 8.1 compatibility.

#6 @lakshyajeet
7 months ago

Reproduction Report

Description

This report validates whether the issue described in Trac #56254 can be reproduced.

Environment

  • WordPress: 7.1-alpha-62161-src
  • PHP: 8.3.30
  • Server: nginx/1.29.7
  • Database: mysqli (Server: 8.4.8 / Client: mysqlnd 8.3.30)
  • Browser: Chrome 146.0.0.0
  • OS: macOS
  • Theme: Twenty Twenty-Five 1.4
  • MU Plugins: None activated
  • Plugins: None activated

Steps to Reproduce

  1. Install and activate the plugin below.
  2. Log in as an administrator and navigate to any wp-admin page.
  3. Observe the PHP warning in the debug log or on screen.

Minimal Reproduction Code

<?php
/**
 * Plugin Name: Reproduce Trac #56254
 */

add_action( 'admin_menu', 'r56254_register_menus' );

function r56254_register_menus(): void {
    add_menu_page(
        'Reproduce #56254',          // page title
        'Reproduce #56254',          // menu title
        'manage_options',            // capability
        'reproduce-56254',           // menu slug
        'r56254_page_callback',      // callback
        'dashicons-warning',         // icon
        80                           // position
    );

    add_submenu_page(
        'reproduce-56254',
        'Good Submenu',
        'Good Submenu',
        'manage_options',            // capability present
        'reproduce-56254-good',
        'r56254_page_callback'
    );

    // Malformed submenu - missing capability
    global $submenu;

    // The array structure core expects: [title, capability, slug, page_title]
    // We deliberately omit index 1 (capability) to trigger the warning.
    $submenu['reproduce-56254'][] = [
        0 => 'Broken Submenu',   // menu title
        // 1 => capability       - INTENTIONALLY MISSING (triggers the bug)
        2 => 'reproduce-56254-broken',  // slug
        3 => 'Broken Submenu',          // page title
    ];
}

function r56254_page_callback(): void {
    echo '<div class="wrap"><h1>Reproduce Trac #56254</h1></div>';
}

Actual Results

  1. ✅ Error condition occurs (reproduced).

Additional Notes

  • The warning is reproducible, but this may be expected behavior on the core side. add_submenu_page() enforces a non-empty capability as a required parameter, meaning the $sub_item[1] slot is always populated under normal usage. The missing key only appears when a plugin or theme bypasses the API and writes directly into the global $submenu array with a malformed entry. This suggests the root cause likely lives in a third-party plugin rather than in WordPress core. The defensive guard proposed in PR would still be a reasonable hardening measure, but the underlying fault is arguably the misbehaving plugin.

This ticket was mentioned in ​PR #14755 on ​WordPress/wordpress-develop by ​@ravipatel.


3 weeks ago
#7

  • Keywords has-unit-tests added; needs-unit-tests removed

Summary

When a plugin adds a submenu item without a capability (for example by modifying $submenu directly), _wp_menu_output() reads $sub_item[1] and PHP emits Warning: Undefined array key 1 in wp-admin/menu-header.php.

This applies the fix from #3140 by @remyperona (refreshed against current trunk) and adds the unit test the ticket was waiting for: such items are now skipped, since they cannot be checked against the current user's capabilities.

Testing

  • New test Tests_Admin_WpMenuOutput::test_submenu_item_without_capability_is_skipped: it fails on trunk with Undefined array key 1 (menu-header.php:247) and passes with the fix. It also checks that a valid sibling submenu item is still listed.
  • phpunit --group menu: OK (164 tests)
  • Tests_Admin_WpMenuOutput on multisite: OK (20 tests)
  • phpcs on both files: no issues

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Finding the ticket, reproducing the warning with a test, applying the existing fix from #3140, and drafting this description. I reviewed the change and ran the tests.

#8 @ravipatel
3 weeks ago

Refreshed the patch from ​https://github.com/WordPress/wordpress-develop/pull/3140 against current trunk and added the missing unit test: ​https://github.com/WordPress/wordpress-develop/pull/14755. The test reproduces "Undefined array key 1" in menu-header.php on trunk and passes with the fix. Props to @remyperona for the original fix.

Last edited 3 weeks ago by ravipatel (previous) (diff)

#9 @ravipatel
29 hours ago

PR ​https://github.com/WordPress/wordpress-develop/pull/14755 is ready for review: it skips submenu items registered without a capability instead of letting them break the admin menu, with unit tests. All checks pass. Could someone from the Administration component take a look? Thanks!

Note: See TracTickets for help on using tickets.