如果不是管理员,则从管理菜单中删除复印机插件

时间:2015-03-18 作者:junkrig

我在删除复印机插件添加的菜单项时遇到了一些问题(https://wordpress.org/plugins/duplicator/) 如果用户不是管理员,请从“管理”菜单中选择。

我可以使用以下代码成功删除非插件项目:

function jitb_remove_admin_menu_items() {
    if ( !current_user_can( \'manage_options\' ) ) {
        $remove_menu_items = array( __( \'Comments\' ), __( \'Links\' ), __( \'Posts\' ), __( \'Media\' ), __( \'Users\' ), __( \'Tools\' ), __( \'Settings\' ), __( \'Profile\' ), __( \'Testimonials\' ), __( \'Appearance\' ), __( \'Portfolio\' ), __( \'Duplicator\' ) );
        global $menu;
        end ( $menu );
        while ( prev( $menu ) ) {
            $item = explode( \' \',$menu[key( $menu )][0] );
            if( in_array( $item[0] != NULL?$item[0]:"" , $remove_menu_items ) ){
                unset( $menu[key( $menu)] );
            }
        }
    }
}

add_action( \'admin_init\', \'jitb_remove_admin_menu_items\' );
但这对“复制器插件”菜单项不起作用。

我还查看了复印机插件代码,它显示了使用此代码设置的菜单项:

function duplicator_menu() {
    $wpfront_caps_translator = \'wpfront_user_role_editor_duplicator_translate_capability\';

    //Main Menu
    $perms = \'export\';
    $perms = apply_filters($wpfront_caps_translator, $perms);
    $main_menu = add_menu_page(\'Duplicator Plugin\', \'Duplicator\', $perms, \'duplicator\', \'duplicator_get_menu\', plugins_url(\'duplicator/assets/img/create.png\'));

    $perms = \'export\';
    $perms = apply_filters($wpfront_caps_translator, $perms);
    $page_packages = add_submenu_page(\'duplicator\', __(\'Packages\', \'wpduplicator\'), __(\'Packages\', \'wpduplicator\'), $perms, \'duplicator\', \'duplicator_get_menu\');

    $perms = \'manage_options\';
    $perms = apply_filters($wpfront_caps_translator, $perms);
    $page_settings = add_submenu_page(\'duplicator\', __(\'Settings\', \'wpduplicator\'), __(\'Settings\', \'wpduplicator\'), $perms, \'duplicator-settings\', \'duplicator_get_menu\');

    $perms = \'manage_options\';
    $perms = apply_filters($wpfront_caps_translator, $perms);
    $page_tools = add_submenu_page(\'duplicator\', __(\'Tools\', \'wpduplicator\'), __(\'Tools\', \'wpduplicator\'), $perms, \'duplicator-tools\', \'duplicator_get_menu\');

    $perms = \'read\';
    $perms = apply_filters($wpfront_caps_translator, $perms);
    $page_help = add_submenu_page(\'duplicator\', __(\'Help\', \'wpduplicator\'), __(\'Help\', \'wpduplicator\'), $perms, \'duplicator-help\', \'duplicator_get_menu\');

    $perms = \'read\';
    $perms = apply_filters($wpfront_caps_translator, $perms);
    $page_about = add_submenu_page(\'duplicator\', __(\'About\', \'wpduplicator\'), __(\'About\', \'wpduplicator\'), $perms, \'duplicator-about\', \'duplicator_get_menu\');

    //Apply Scripts
    add_action(\'admin_print_scripts-\' . $page_packages, \'duplicator_scripts\');
    add_action(\'admin_print_scripts-\' . $page_settings, \'duplicator_scripts\');
    add_action(\'admin_print_scripts-\' . $page_help, \'duplicator_scripts\');
    add_action(\'admin_print_scripts-\' . $page_tools, \'duplicator_scripts\');
    add_action(\'admin_print_scripts-\' . $page_about, \'duplicator_scripts\');

    //Apply Styles
    add_action(\'admin_print_styles-\' . $page_packages, \'duplicator_styles\');
    add_action(\'admin_print_styles-\' . $page_settings, \'duplicator_styles\');
    add_action(\'admin_print_styles-\' . $page_help, \'duplicator_styles\');
    add_action(\'admin_print_styles-\' . $page_tools, \'duplicator_styles\');
    add_action(\'admin_print_styles-\' . $page_about, \'duplicator_styles\');
} 
但是,在我的jitb\\u remove\\u admin\\u menu\\u items函数中添加标题“replicator”不会删除菜单项。

在这种情况下,我做错了什么?

谢谢

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

经过进一步研究,我在这篇文章中找到了一种方法https://wordpress.stackexchange.com/a/136064/54989

而不是使用unset 正如我之前所做的那样,remove_menu_page works 对于复印机:

remove_menu_page( \'duplicator\' );
现在,我的完整代码如下所示:

function jitb_remove_admin_menu_items() {
    if ( !current_user_can( \'manage_options\' ) ) {
        $remove_menu_items = array( __( \'Comments\' ), __( \'Links\' ), __( \'Posts\' ), __( \'Media\' ), __( \'Users\' ), __( \'Tools\' ), __( \'Settings\' ), __( \'Profile\' ), __( \'Testimonials\' ), __( \'Appearance\' ), __( \'Portfolio\' ),  __( \'Products\' ), __( \'Orders\' ), __( \'Coupons\' ) );
        global $menu;
        end ( $menu );
        while ( prev( $menu ) ) {
            $item = explode( \' \',$menu[key( $menu )][0] );
            if( in_array( $item[0] != NULL?$item[0]:"" , $remove_menu_items ) ){
                unset( $menu[key( $menu)] );
            }
        }
        remove_menu_page( \'duplicator\' );
    }
}

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

结束

相关推荐

Single必须使用Plugins目录进行本地开发

我有多个本地安装的WordPress,并希望有一个单一的必须使用插件目录为我的所有本地网站。有什么我可以添加到wp配置,例如,让我有一个文件夹,可以用于我的所有网站?或者另一种方法?例如:/根/站点/站点1//根/站点/站点2//根/站点/站点3/。。。所有用途:/根/mu插件/谢谢