Make WordPress Core

Opened 15 months ago

Last modified 6 months ago

#61962 new defect (bug)

Admin: remove_menu_page throws PHP warning when $menu is null

Reported by: anonymized_15266067's profile anonymized_15266067 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Plugins Keywords: has-patch reporter-feedback
Focuses: administration Cc:

Description (last modified by ironprogrammer)

error.log:

[Thu Aug 29 21:48:07.078821 2024] [php:warn] [pid 16202] [client 172.69.58.130:44688] PHP Warning:  foreach() argument must be of type array|object, null given in /var/www/example.com/shop.example.com/wp-admin/includes/plugin.php on line 1853, referer: https://shop.example.com/wp-admin/plugins.php?plugin_status=all&paged=1&s

[Thu Aug 29 21:48:20.875148 2024] [php:warn] [pid 16197] [client 172.69.59.57:34548] PHP Warning:  foreach() argument must be of type array|object, null given in /var/www/example.com/www.example.com/wp-admin/includes/plugin.php on line 1853, referer: https://example.com/wp-admin/

[Thu Aug 29 21:49:08.193887 2024] [php:warn] [pid 16191] [client 172.69.58.29:63630] PHP Warning:  foreach() argument must be of type array|object, null given in /var/www/example.com/shop.example.com/wp-admin/includes/plugin.php on line 1853, referer: https://shop.example.com/wp-admin/index.php

[Thu Aug 29 21:50:08.131920 2024] [php:warn] [pid 16388] [client 172.69.58.223:63418] PHP Warning:  foreach() argument must be of type array|object, null given in /var/www/example.com/shop.example.com/wp-admin/includes/plugin.php on line 1853, referer: https://shop.example.com/wp-admin/plugins.php?plugin_status=all&paged=1&s

[Thu Aug 29 21:50:21.769929 2024] [php:warn] [pid 16193] [client 172.69.58.231:25962] PHP Warning:  foreach() argument must be of type array|object, null given in /var/www/example.com/www.example.com/wp-admin/includes/plugin.php on line 1853, referer: https://example.com/wp-admin/

[Thu Aug 29 21:51:08.308475 2024] [php:warn] [pid 16384] [client 172.69.58.183:64764] PHP Warning:  foreach() argument must be of type array|object, null given in /var/www/example.com/shop.example.com/wp-admin/includes/plugin.php on line 1853, referer: https://shop.example.com/wp-admin/index.php

[Thu Aug 29 21:52:22.795278 2024] [php:warn] [pid 16202] [client 172.69.58.24:28566] PHP Warning:  foreach() argument must be of type array|object, null given in /var/www/example.com/www.example.com/wp-admin/includes/plugin.php on line 1853, referer: https://example.com/wp-admin/

[Thu Aug 29 21:53:05.970841 2024] [php:warn] [pid 16197] [client 172.69.58.163:36022] PHP Warning:  foreach() argument must be of type array|object, null given in /var/www/example.com/shop.example.com/wp-admin/includes/plugin.php on line 1853, referer: https://shop.example.com/wp-admin/index.php

[Thu Aug 29 21:53:08.902748 2024] [php:warn] [pid 16197] [client 172.69.58.163:36022] PHP Warning:  foreach() argument must be of type array|object, null given in /var/www/example.com/shop.example.com/wp-admin/includes/plugin.php on line 1853, referer: https://shop.example.com/wp-admin/plugins.php?plugin_status=all&paged=1&s

[Thu Aug 29 21:53:23.836918 2024] [php:warn] [pid 16191] [client 172.69.59.91:51414] PHP Warning:  foreach() argument must be of type array|object, null given in /var/www/example.com/www.example.com/wp-admin/includes/plugin.php on line 1853, referer: https://example.com/wp-admin/

Change History (7)

This ticket was mentioned in PR #7273 on WordPress/wordpress-develop by @narenin.


15 months ago
#1

  • Keywords has-patch added; needs-patch removed

#2 @narenin
15 months ago

Hi @xbigx

Welcome to the Core Trac!

This warning is coming because null is passed to foreach. I have created the PR with patch for the same.

Thanks for the detailed information.

#3 @ironprogrammer
14 months ago

  • Description modified (diff)
  • Keywords reporter-feedback added
  • Summary changed from error.log - cant figure out issue with Google Search.. to Admin: remove_menu_page throws PHP warning when $menu is null

Welcome to Trac, @xbigx, and thanks for the report! And thank you, @narenin, for a proposed patch!

It would be really helpful if testing instructions were provided so that other contributors could attempt to reproduce the error, and then validate the patch. I'm not able to reproduce this error using a vanilla install of WordPress.

I'm marking this ticket with reporter-feedback to request additional information from @xbigx, such as environment or plugin information that may point to how remove_menu_page is being called. For instance, if it's being called before the $menu global has been initialized (like in the plugins_loaded hook), then it may point toward a different problem.

I've also updated the ticket title for clarity.

#4 @hellofromTonya
14 months ago

  • Version 6.6.1 deleted

I too am unable to reproduce this reported issue.

Also, not seeing any changes made in 6.6.1 release that might contribute the reported issue. Removing it as the Version.

#5 @arnolp
12 months ago

reproducing the issue on WordPress 6.7
All my remove_menuhooks are on 'admin_menu' or 'admin_init'

Last edited 12 months ago by arnolp (previous) (diff)

#6 @ironprogrammer
12 months ago

Thanks, @arnolp! Would you be able to provide some testing instructions to help reproduce the error? As noted in comment:3 and comment:4, additional context of the environment, plugins (even snippets), etc would help move this forward.

#7 @njwp
6 months ago

same issue with me. I suspect it is if woocommerce is installed.
I use admin_init
As far as I can see admn_init seems to be run twice - once when the menu exists and once when it doesn't. So the menu items are actually correctly removed (in the instance when the menu has a value) but the error is thrown in the instance when the menu doesn't have a value. I could be totally wrong but that's what it feels like.

Instead of using remove_menu_page, I just used the code from that function in my functions.php and wrapped it with

if($menu){}

Works perfectly.

Compare to the remove_submenu_page function which DOES check for existence of the menu item:

if ( ! isset( $submenu[ $menu_slug ] ) ) {

return false;

}


Note: See TracTickets for help on using tickets.