从外观管理菜单中删除自定义背景和页眉,不带CSS或JS

时间:2015-06-02 作者:Fredrik

我试图从外观菜单中删除背景和标题,但它们似乎并没有消失!我想这是因为我已经激活了自定义,但我是否可以在不使用CSS或JS的情况下删除它们?

这是我的代码:

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

function remove_unnecessary_wordpress_menus(){
    remove_menu_page(\'themes.php?page=custom-background\');
    remove_submenu_page(\'themes.php\', \'custom-background\');
    remove_submenu_page(\'themes.php\', \'custom-header\');
}
提前感谢!

5 个回复
最合适的回答,由SO网友:James Cushing 整理而成

虽然听起来过于复杂,但我总是发现处理管理菜单修改的最佳方法是忽略给定的wordpressremove_ 函数并直接转到$menu$submenu 全局。在此处指定的情况下,您希望将代码更改为:

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

function remove_unnecessary_wordpress_menus(){
    global $submenu;
    unset($submenu[\'themes.php\'][20]);
    unset($submenu[\'themes.php\'][22]);
}
中页面的索引themes.php 数组看起来很奇怪,但当你试图破解WP时,有什么不是呢?!可以找到使用这些全局变量的良好参考here.

编辑:只是一个想法,考虑到插件数量的变化(可能,但不一定)会改变数组中给定菜单/子菜单项的索引,如果我提供的代码片段不起作用,最好检查所需的数字。您可以通过将代码稍微修改为:

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

function remove_unnecessary_wordpress_menus(){
    global $submenu;
    //Left margin is to account for the admin sidebar menu
    echo \'<pre style="margin-left:11em">\';
    print_r($submenu);
    echo \'</pre>\';
}
这将“漂亮”地打印$submenu 数组,从中可以找到所需的确切数字。

编辑:因为我还没有代表对这个社区发表评论,所以值得指出的是@Fredrik在推广方面做得很好+1.

SO网友:Fredrik

这是我最后的代码。谢谢你的快速回答!

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

function remove_unnecessary_wordpress_menus(){
    global $submenu;
    foreach($submenu[\'themes.php\'] as $menu_index => $theme_menu){
        if($theme_menu[0] == \'Header\' || $theme_menu[0] == \'Background\')
        unset($submenu[\'themes.php\'][$menu_index]);
    }
}

SO网友:Yutaro Ikeda

谢谢大家!以下是WordPress 4.9.8中的代码。

function remove_header_and_bg(){
  global $submenu;
  unset($submenu[\'themes.php\'][6]); // customize
  unset($submenu[\'themes.php\'][15]); // header_image
  unset($submenu[\'themes.php\'][20]); // background_image
}
add_action( \'admin_menu\', \'remove_header_and_bg\', 999 );

SO网友:Paal Joachim Romdahl

下面是另一个删除标题和背景的选项(source):

//Remove the custom options provided by the default twentyeleven theme.     
add_action( \'after_setup_theme\',\'remove_twentyeleven_options\', 100 );
function remove_twentyeleven_options() {    
    remove_custom_background();
    remove_custom_image_header();
    remove_action(\'admin_menu\', \'twentyeleven_theme_options_add_page\');    
}

SO网友:KittMedia

无需使用循环的另一个版本是使用正确的标识符,该标识符可以通过以下方式显示:var_dump 全球$submenu.

function remove_unnecessary_wordpress_menus(){
    remove_submenu_page( \'themes.php\', \'customize.php?return=%2Fwp-admin%2Findex.php&#038;autofocus%5Bcontrol%5D=header_image\' );
    remove_submenu_page( \'themes.php\', \'customize.php?return=%2Fwp-admin%2Findex.php&#038;autofocus%5Bcontrol%5D=background_image\' );
}

add_action(\'admin_menu\', \'remove_unnecessary_wordpress_menus\', 999);
仅当网站路径为/, 否则,需要在customize.php?return=, e、 g。customize.php?return=%2Fwordpress%2F 如果路径为/wordpress/.

还可以自动处理路径:

function remove_unnecessary_wordpress_menus(){
    $site_path = rawurlencode( get_blog_details()->path );

    remove_submenu_page( \'themes.php\', \'customize.php?return=\' . $site_path . \'wp-admin%2Findex.php&#038;autofocus%5Bcontrol%5D=header_image\' );
    remove_submenu_page( \'themes.php\', \'customize.php?return=\' . $site_path . \'wp-admin%2Findex.php&#038;autofocus%5Bcontrol%5D=background_image\' );
}

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

结束