#58403 closed defect (bug) (worksforme)
Running remove_menu_page in the theme will cause admin-ajax.php 500 errors
Reported by: | 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:
↓ 2
@
15 months ago
- Component changed from Plugins to Menus
- Severity changed from blocker to normal
#2
in reply to:
↑ 1
@
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 theadmin_init
action.
According to the documentation for this function, you should be using theadmin_menu
action instead to avoid the error.
Theadmin_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.
Note: See
TracTickets for help on using
tickets.
Hello @niau and welcome to trac.
Looking at your example, you are calling the function
remove_menu_page
during theadmin_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 ?