Make WordPress Core

Opened 15 months ago

Closed 15 months ago

Last modified 6 months ago

#58403 closed defect (bug) (worksforme)

Running remove_menu_page in the theme will cause admin-ajax.php 500 errors

Reported by: niau's profile niau Owned by:
Milestone: Priority: normal
Severity: normal Version: 6.2.2
Component: Menus Keywords:
Focuses: Cc:

Description

<?php
  add_action('admin_init', function () {
    current_user_can('administrator') || remove_menu_page('tools.php');
  });

When I log in with administrator, everything is fine, but when I switch to another user, I can't get into Media Manager.

Check the source code and find that '/wp-admin/includes/plugin.php' of 'remove_menu_page' does not check if '$menu' is empty.

<?php
  function remove_menu_page( $menu_slug ) {
    global $menu;

    if (!empty($menu)) {
      foreach ( $menu as $i => $item ) {
        if ( $menu_slug === $item[2] ) {
          unset( $menu[ $i ] );
          return $item;
        }
      }
    }


    return false;
  }

Change History (4)

#1 follow-up: @petitphp
15 months ago

  • Component changed from Plugins to Menus
  • Severity changed from blocker to normal

Hello @niau and welcome to trac.

Looking at your example, you are calling the function remove_menu_page during the admin_init action.
According to the documentation for this function, you should be using the admin_menu action instead to avoid the error.
The admin_init action is too early, and the menu is not ready to be manipulated at this point.

Can you try to change the hook and see if you still have the issue ?

#2 in reply to: ↑ 1 @niau
15 months ago

Replying to petitphp:

Hello @niau and welcome to trac.

Looking at your example, you are calling the function remove_menu_page during the admin_init action.
According to the documentation for this function, you should be using the admin_menu action instead to avoid the error.
The admin_init action is too early, and the menu is not ready to be manipulated at this point.

Can you try to change the hook and see if you still have the issue ?

Thanks, switching to 'admin_menu' solved my problem.

#3 @petitphp
15 months ago

  • Resolution set to worksforme
  • Status changed from new to closed

Thanks for the follow-up.

Closing the ticket.

#4 @swissspidy
6 months ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.