Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove submenu page "customize.php" in WordPress 4.0

Tags:

wordpress

When I was running WP 3.9.2 I was able to use the following code to remove the Customize menu item from Appearance in the admin menu.

function remove_customize() {
  remove_submenu_page('themes.php', 'customize.php');
}
add_action('admin_init', 'remove_customize', 999);

Once I updated to 4.0 this is no longer working.

like image 856
isabisa Avatar asked Sep 11 '14 13:09

isabisa


3 Answers

Answer should be:

add_action( 'admin_menu', function () {
global $submenu;
if ( isset( $submenu[ 'themes.php' ] ) ) {
    foreach ( $submenu[ 'themes.php' ] as $index => $menu_item ) {
        foreach ($menu_item as $value) {
            if (strpos($value,'customize') !== false) {
                unset( $submenu[ 'themes.php' ][ $index ] );
            }
        }
    }
}
});

The way rjb used an array as the needle in in_array() in the accepted answer doesn't work. Check out why in the docs. I replaced in_array with another foreach that loops through the $menu_item arrays and looks for 'customize' as part of the value.

Works for me with WordPress 4.9.6

like image 70
bash88 Avatar answered Dec 21 '22 01:12

bash88


This works with WordPress 4.1 and 4.0 and 3.x here:

Edit: Adjusted for WordPress 4.1 compatibility:

function remove_customize() {
    $customize_url_arr = array();
    $customize_url_arr[] = 'customize.php'; // 3.x
    $customize_url = add_query_arg( 'return', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'customize.php' );
    $customize_url_arr[] = $customize_url; // 4.0 & 4.1
    if ( current_theme_supports( 'custom-header' ) && current_user_can( 'customize') ) {
        $customize_url_arr[] = add_query_arg( 'autofocus[control]', 'header_image', $customize_url ); // 4.1
        $customize_url_arr[] = 'custom-header'; // 4.0
    }
    if ( current_theme_supports( 'custom-background' ) && current_user_can( 'customize') ) {
        $customize_url_arr[] = add_query_arg( 'autofocus[control]', 'background_image', $customize_url ); // 4.1
        $customize_url_arr[] = 'custom-background'; // 4.0
    }
    foreach ( $customize_url_arr as $customize_url ) {
        remove_submenu_page( 'themes.php', $customize_url );
    }
}
add_action( 'admin_menu', 'remove_customize', 999 );
like image 25
Ov3rfly Avatar answered Dec 21 '22 01:12

Ov3rfly


You can directly modify the $submenus global like so:

global $submenu;
unset($submenu['themes.php'][6]); // Customize link

I'm using this in the same function, hooked into admin_menu, as I use to unset other admin items and it seems to be working fine

function as_remove_menus () {
       remove_menu_page('upload.php'); //hide Media
       remove_menu_page('link-manager.php'); //hide links
       remove_submenu_page( 'edit.php', 'edit-tags.php' ); //hide tags
       global $submenu;
        // Appearance Menu
        unset($submenu['themes.php'][6]); // Customize
}
add_action('admin_menu', 'as_remove_menus');
like image 22
Andrew Cafourek Avatar answered Dec 21 '22 02:12

Andrew Cafourek