0

I'm having a bit of trouble removing the menu item added by the Duplicator plugin (https://wordpress.org/plugins/duplicator/) from the admin menu if the user is not an administrator.

I am able to remove non-plugin items successfully by using the following code:

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' );

but this isn't working for the Duplicator plugin menu item.

I have also had a look at the Duplicator plugin code, and it is showing the menu item being set up with this code:

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');
} 

however, adding the title 'Duplicator' to my jitb_remove_admin_menu_items function doesn't remove the menu item.

What am I doing wrong in this case?

Thanks,

0

After a bit more research, I have found a way to do this from this post https://wordpress.stackexchange.com/a/136064/54989

Rather than using unset as I had done previously, remove_menu_page works for Duplicator:

remove_menu_page( 'duplicator' );

so my complete code now looks like:

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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.