The wordpress function remove_menu_page () throws an error

I am trying to delete several top-level menus in the Wordpress admin panel. Oddly enough, I get an error message from the plugin.php file where the function is declared: "Invalid argument provided by foreach () in C: \ wamp \ www \ wordpress-alut \ wp-admin \ includes \ plugin.php on line 1261. I went to the file and found the following code:

function remove_menu_page( $menu_slug ) { global $menu; foreach ( $menu as $i => $item ) { // **this is line 1261** if ( $menu_slug == $item[2] ) { unset( $menu[$i] ); return $item; } } return false; } 

It is important to note that when I use remove_ the _page () submenu , which is the next function in plugin.php, I do not get such an error. my function located in functions.php:

 add_action( 'admin_init', 'mf_remove_menu_pages' ); function mf_remove_menu_pages() { remove_menu_page('link-manager.php'); remove_menu_page('index.php'); remove_menu_page('users.php'); remove_menu_page('upload.php'); remove_menu_page('tools.php'); remove_menu_page('edit.php'); remove_menu_page('edit-comments.php'); remove_menu_page('post-new.php'); remove_submenu_page('themes.php','themes.php'); remove_submenu_page('themes.php','theme-editor.php'); remove_submenu_page('themes.php','widgets.php'); }; 
+8
php wordpress menu
source share
2 answers

You call mf_remove_menu_pages() before the $menu really set up, or it seems like in cases where the $menu never set up. You need to wait for the admin_menu hook to actually call remove_menu_page() . If you do this on admin_init then you are using the wrong hook and therefore it should work on AJAX requests

So you need to change the hook. Try:

 add_action( 'admin_menu', 'mf_remove_menu_pages' ); function mf_remove_menu_pages() { remove_menu_page('link-manager.php'); remove_menu_page('index.php'); remove_menu_page('users.php'); remove_menu_page('upload.php'); remove_menu_page('tools.php'); remove_menu_page('edit.php'); remove_menu_page('edit-comments.php'); remove_menu_page('post-new.php'); remove_submenu_page('themes.php','themes.php'); remove_submenu_page('themes.php','theme-editor.php'); remove_submenu_page('themes.php','widgets.php'); } 

Hope this helps :)

+17
source share

In newer versions of WordPress, you must bind to the admin_menu hook with a very high priority to remove some subpages, such as theme-editor.php (or subpages represented by the Jetpack plugin).

eg. to remove theme-editor.php you need to set the priority to about 120 . For Jetpack pages you need something around 2000 .

 add_action( 'admin_menu', 'mf_remove_menu_pages', 120 ); function mf_remove_menu_pages() { remove_menu_page('link-manager.php'); remove_menu_page('index.php'); remove_menu_page('users.php'); remove_menu_page('upload.php'); remove_menu_page('tools.php'); remove_menu_page('edit.php'); remove_menu_page('edit-comments.php'); remove_menu_page('post-new.php'); remove_submenu_page('themes.php','themes.php'); remove_submenu_page('themes.php','theme-editor.php'); remove_submenu_page('themes.php','widgets.php'); } 

This is also indicated in the Wordpress Codex and in the Wordpress Support Forum .

0
source share

All Articles