WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 weeks ago

#43714 closed defect (bug) (duplicate)

remove_menu_page(): E_WARNING: Invalid argument supplied for foreach()

Reported by: conner_bw Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.9.5
Component: Administration Keywords:
Focuses: Cc:

Description

remove_menu_page() sometimes logs: E_WARNING: Invalid argument supplied for foreach()

Problematic code:

function remove_menu_page( $menu_slug ) {
  global $menu;
  foreach ( $menu as $i => $item ) {
  // ...

URL /wp/wp-admin/admin-ajax.php
HTTP referrer https://_SOME_SITE_/wp/wp-admin/network/users.php

Stack trace

/vendor/soberwp/intervention/src/Module/RemoveMenuItems.php (121)
/web/wp/wp-includes/class-wp-hook.php (286)
/web/wp/wp-includes/class-wp-hook.php (310)
/web/wp/wp-includes/plugin.php (453)
/web/wp/wp-admin/admin-ajax.php (44)

Offending line:
https://github.com/soberwp/intervention/blob/7d69c58b32fce29aad08e4ff9c9de43963122610/src/Module/RemoveMenuItems.php#L121

Possible reason: global $menu not defined?

Change History (5)

#1 @welcher
2 years ago

  • Keywords reporter-feedback added

@conner_bw thanks for the ticket! It looks like you're calling remove_menu_item() via a custom framework. Are you able to reproduce this error with a clean install of WordPress without any third-party code?

#2 @conner_bw
2 years ago

I could not reproduce without third-party code.

I fixed it by changing:

require get_template_directory() . '/inc/intervention.php';

To:

if ( ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
	require get_template_directory() . '/inc/intervention.php';
}

#3 @conner_bw
2 years ago

  • Keywords reporter-feedback removed

#4 @SergeyBiryukov
2 years ago

  • Component changed from Menus to Administration
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Hi @conner_bw, thanks for the ticket!

It looks like the warning here indicates a developer error. I don't think we should hide developer errors, a warning seems valid here.

#5 @SergeyBiryukov
2 weeks ago

  • Resolution changed from invalid to duplicate

Duplicate of #23767.

Note: See TracTickets for help on using tickets.