PHP 8 warning – Undefined array key 2 in Admin_Menus.php
-
Hi,
On one of our sites we are repeatedly seeing the following warning in the server error log when accessing the WordPress admin:
PHP Warning: Undefined array key 2 in
/wp-content/plugins/white-label-cms/includes/classes/Admin_Menus.php on line 368Environment:
- WordPress: 6.9.4
- PHP: 8.3.30
- White Label CMS: 2.7.8
- Theme: JNews 12.0.4
- Other relevant plugins: WooCommerce / Yoast / etc. (if applicable)
Looking at the code of
WLCMS_Admin_Menus::compile_menus(), it seems the plugin is accessing$menu_item[2]without checking that this index actually exists:foreach ( $menu as $menu_item ) {
// some menu items are seperators, skip them
if ( $menu_item[0] == '' ) {
continue;
}
if ( $menu_item[2] == $sidebar_url ) {
continue;
}
$menu_name = preg_replace('#(<span.*?>).*?(</span>)#', '', $menu_item[0]);
$menu_key = $menu_item[2];
...
}
On PHP 8, whenever a$menuentry does not define index2, this results in the exact warning mentioned above.Suggested fix:
foreach ( $menu as $menu_item ) {
if ( ! isset( $menu_item[0], $menu_item[2] ) ) {
continue;
}
if ( $menu_item[0] === '' ) {
continue;
}
if ( $menu_item[2] === $sidebar_url ) {
continue;
}
$menu_name = preg_replace('#(<span.*?>).*?(</span>)#', '', $menu_item[0]);
$menu_key = $menu_item[2];
...
}Adding a simple
isset()guard prevents the warning without changing the business logic, and only skips malformed menu entries.Could you please confirm whether this approach looks correct to you and if you plan to include a similar fix in an upcoming version of the plugin?
Thanks in advance.
The page I need help with: [log in to see the link]
You must be logged in to reply to this topic.