如何删除插件插入的管理菜单页面?

时间:2014-02-26 作者:tmyie

我有以下代码,它清理了许多管理区域中不会使用的内容:

add_action( \'admin_menu\', \'my_remove_menu_pages\' );

function my_remove_menu_pages() {
  remove_menu_page( \'edit.php\' );                   //Posts
  remove_menu_page( \'upload.php\' );                 //Media
  remove_menu_page( \'edit-comments.php\' );          //Comments
  remove_menu_page( \'themes.php\' );                 //Appearance
  remove_menu_page( \'users.php\' );                  //Users
  remove_menu_page( \'tools.php\' );                  //Tools
  remove_menu_page( \'options-general.php\' );        //Settings

};
但是,有两个菜单项是从插件插入的。

当我将鼠标悬停在每个菜单项上时,它会显示以下链接:

/wp-admin/edit.php?post_type=acf
/wp-admin/admin.php?page=wpcf7
有没有办法隐藏这些菜单页面?

7 个回复
最合适的回答,由SO网友:tfrommen 整理而成

您需要使用正确的钩子(不总是与URL/slug相同),并且使用稍后运行的钩子(例如。,admin_init):

add_action( \'admin_init\', \'wpse_136058_remove_menu_pages\' );

function wpse_136058_remove_menu_pages() {

    remove_menu_page( \'edit.php?post_type=acf\' );
    remove_menu_page( \'wpcf7\' );
}
可以使用以下方法进行调试:

add_action( \'admin_init\', \'wpse_136058_debug_admin_menu\' );

function wpse_136058_debug_admin_menu() {

    echo \'<pre>\' . print_r( $GLOBALS[ \'menu\' ], TRUE) . \'</pre>\';
}
这为联系人表单7插件菜单页提供了以下内容(对于我的设置):

[27] => Array
        (
            [0] => Formular
            [1] => wpcf7_read_contact_forms
            [2] => wpcf7
            [3] => Contact Form 7
            [4] => menu-top menu-icon-generic toplevel_page_wpcf7 menu-top-last
            [5] => toplevel_page_wpcf7
            [6] => none
        )
具有键的数组元素2 是您想要的:wpcf7.

SO网友:hereswhatidid

您可能需要设置add\\u操作priority 以更高的数字为目标,专门针对这两个新链接:

add_action( \'admin_menu\', \'my_remove_menu_pages\', 999 );

function my_remove_menu_pages() {
  remove_menu_page( \'edit.php\' );                   //Posts
  remove_menu_page( \'upload.php\' );                 //Media
  remove_menu_page( \'edit-comments.php\' );          //Comments
  remove_menu_page( \'themes.php\' );                 //Appearance
  remove_menu_page( \'users.php\' );                  //Users
  remove_menu_page( \'tools.php\' );                  //Tools
  remove_menu_page( \'options-general.php\' );        //Settings
  remove_menu_page( \'edit.php?post_type=acf\' );
  remove_menu_page( \'wpcf7\' );
};
高级自定义字段还提供了一个帮助文档,说明如何在此处为其菜单执行此操作:

http://www.advancedcustomfields.com/resources/how-to/how-to-hide-acf-menu-from-clients/

联系人表单7也有自己的方式限制菜单的可见性:

http://contactform7.com/restricting-access-to-the-administration-panel/

SO网友:Bryan Willis

    // PLUGINS THAT ADD AS DASHBOARD SUBMENU
    // IF THIS IS THE URL BELOW - THE PAGE VARIABLE IS WHAT I NEED ( SO WHATEVER COMES AFTER PAGE= )
    // http://example.com/wp-admin/index.php?page=iconize-plugin-update-notifier
    remove_submenu_page( \'index.php\', \'iconize-plugin-update-notifier\' );

    // OR FOR EXAMPLE WOOCOMMERCE DASHBOARD SUBMENU
    remove_submenu_page( \'index.php\', \'wc-about\' ); //WOO
    remove_submenu_page( \'index.php\', \'wc-credits\' ); //WOO
    remove_submenu_page( \'index.php\', \'wc-translators\' ); //WOO

    // CUSTOM POST TYPE TOP LEVELS
    remove_menu_page( \'edit.php?post_type={$POST_TYPE}\' ); //LOOK FOR WHAT COMES AFTER POST TYPE IN THE URL
    remove_menu_page( \'edit.php?post_type=testimonials-widget\' ); //TESTIMONIALS WIDGET
    remove_menu_page( \'edit.php?post_type=product\' ); //WOOCOMMERCE

    // CUSTOM POST TYPE SUBMENU
     remove_submenu_page( \'edit.php?post_type={$POST_TYPE}\', \'{$SUBMENU_URL_VARIABLE}\' ); //EXAMPLE FORMAT
     // SO IF BELOW IS THE URL
     // http://example.com/wp-admin/edit.php?post_type=testimonials-widget&page=testimonialswidget_settings
     // YOU NEED TO SEE WHATS AFTER PAGE
     remove_submenu_page( \'edit.php?post_type=testimonials-widget\', \'testimonialswidget_settings\' ); //TESTIMONIALS WIDGET

    // OTHER EXAMPLES
    remove_menu_page( \'revslider\' ); // REVSLIDER
    remove_menu_page( \'woocommerce\' ); // WOOCOMMERCE
    remove_menu_page( \'order-post-types-shop_order\' ); // WOOCOMMERCE
    remove_menu_page( \'order-post-types-shop_coupons\' ); // WOOCOMMERCE
    remove_menu_page( \'shortcodes-ultimate\' ); // SHORTCODES ULTIMATE
    remove_menu_page( \'wp-admin-microblog/wp-admin-microblog.php\' ); // ADMIN MICROBLOG
    remove_menu_page( \'snippets\' ); //CODE SNIPPETS
    remove_menu_page( \'gf_edit_forms\' ); // GRAVITY FORMS
        remove_submenu_page( \'gf_edit_forms\', \'gf_settings\' ); // GRAVITY FORMS
        remove_submenu_page( \'gf_edit_forms\', \'gf_export\' ); // GRAVITY FORMS
        remove_submenu_page( \'gf_edit_forms\', \'gf_update\' ); // GRAVITY FORMS
        remove_submenu_page( \'gf_edit_forms\', \'gf_addons\' ); // GRAVITY FORMS
        remove_submenu_page( \'gf_edit_forms\', \'gf_help\' ); // GRAVITY FORMS

        remove_submenu_page( \'cleverness-to-do-list\', \'cleverness-to-do-list-settings\' ); //Cleverness TODO
您可以使用以下内容调试所有这些内容,以获取所需的所有信息:

if (!function_exists(\'debug_admin_menus\')):
function debug_admin_menus() {
if ( !is_admin())
        return;
    global $submenu, $menu, $pagenow;
    if ( current_user_can(\'manage_options\') ) { // ONLY DO THIS FOR ADMIN
        if( $pagenow == \'index.php\' ) {  // PRINTS ON DASHBOARD
            echo \'<pre>\'; print_r( $menu ); echo \'</pre>\'; // TOP LEVEL MENUS
            echo \'<pre>\'; print_r( $submenu ); echo \'</pre>\'; // SUBMENUS
        }
    }
}
add_action( \'admin_notices\', \'debug_admin_menus\' );
endif;
这些键将为您提供数组值,允许您使用wordpress全局变量执行所有这些操作(尽管不推荐)

function remove_submenus_all_please() {
if ( !is_admin())
        return;
  global $submenu;
    unset($submenu[\'index.php\'][10]); // Removes Updates
  //Posts menu
 // unset($submenu[\'edit.php\'][5]); // Leads to listing of available posts to edit
 // unset($submenu[\'edit.php\'][10]); // Add new post
 // unset($submenu[\'edit.php\'][15]); // Remove categories
 // unset($submenu[\'edit.php\'][16]); // Removes Post Tags
}
add_action(\'admin_menu\', \'remove_submenus_all_please\', 999);
要为某些用户删除,只需执行相同的操作,只需添加功能即可:

function remove_by_caps_admin_menu() {
if ( !is_admin())
        return;
    if ( !current_user_can(\'manage_options\') ) {
        remove_menu_page( \'revslider\' ); // REVSLIDER
    } elseif ( !current_user_can(\'edit_posts\') ) {
        remove_menu_page( \'woocommerce\' ); // WOO
    } else {
    }
}
add_action(\'admin_menu\', \'remove_by_caps_admin_menu\', 999);
为了把它们联系在一起,为什么不缩短我们的代码?可以使用数组避免将remove\\u submenu\\u page写入50次。这也适用于工具栏节点:

if ( !function_exists( \'remove_admin_menupages_in_array\' ) ) {
function remove_admin_menupages_in_array() {
    if ( !is_admin())
        return;
          $admin_menus = array(
                                \'revslider\',
                                \'woocommerce\',
                                \'shortcodes-ultimate\',
          );

    foreach ( $admin_menus as $menu_page ) {
        if ( !current_user_can(\'manage_options\') ) {
            remove_menu_page( $menu_page );
        }
    } // foreach
 } // function
add_action(\'admin_menu\', \'remove_admin_menupages_in_array\', 9999);
} // exists

SO网友:numediaweb

Update

created a code snippet 具有处理主菜单和子菜单项的更强大功能。

Original answer

指定要保留的菜单,而不是指定要删除的菜单:)

add_action(\'admin_init\', \'nwcm_admin_init\');

function nwcm_admin_init()
{   
    // Remove unnecessary menus 
    $menus_to_stay = array(
        // Client manager
        \'nwcm\',

        // Dashboard
        \'index.php\',

        // Users
        \'users.php\'
    );      
    foreach ($GLOBALS[\'menu\'] as $key => $value) {          
        if (!in_array($value[2], $menus_to_stay)) remove_menu_page($value[2]);
    }   

} 
这样,您就不必在添加新插件时搜索插件名称和修改代码。。

SO网友:Vincent Wong

您需要找到正确的$menu\\u slug。以下代码适用于我:

add_action( \'admin_init\', \'remove_menus\' );
function remove_menus(){
  remove_menu_page( \'wpcf7\' );
}

SO网友:Prabhu Chelladurai

当然,您只能指定要删除的项目。查看以下代码:

/wordpress/wp-content/plugins/your_plugin/your_plugin.php :


add_action(\'admin_init\', \'nwcm_admin_init\');

function nwcm_admin_init()
{   
// Remove unnecessary menus 
$menus_to_remove = array(
    // menu items you want to remove
    \'menu_item_1\',
    \'menu_item_2\',
    .
    .
    \'menu_item_n\'
);     

// To avoid warning message, check whether \'menu\' is set AND is an array
if(isset($GLOBALS[\'menu\']) && is_array($GLOBALS[\'menu\'])) {
   foreach ($GLOBALS[\'menu\'] as $key => $value) {  
     //Remove the \'!\' in the \'if\' condition.        
     if (in_array($value[2], $menus_to_remove)) remove_menu_page($value[2]);
   }
}   

} 
这只是numediaweb方法的缺点。感谢@numediaweb。它工作得很好。

P、 S.:\'menu\\u item\\u 1/n\'->将鼠标指向菜单项,并获取链接中显示的该菜单项的确切页面。

SO网友:DoubleAsam

使用此代码段

function your_custom_name() {    
    remove_menu_page(\'vc-general\'); //the slug as seen wp-admin.php?page=vc-general  
}    
add_action( \'admin_init\', \'your_custom_name\' );

结束

相关推荐

自定义分类Meta Admin列

我添加了一个自定义的分类法-shop\\u departments,它是分层的。然后,我在这个新的分类法中添加了一个元字段-term\\u meta[首页]-这一切都很好。然而,在分类法的管理屏幕上,我需要一列元数据,这样用户可以一目了然地看到哪些部门被分配到了首页。我不知道如何在这个页面上添加列。我想做如下事情:// Register the column function front_page_column_register( $columns ) { $columns[\'front