在运行WP 3.9.2时,我能够使用以下代码从管理菜单中的外观中删除自定义菜单项。
function remove_customize() {
remove_submenu_page('themes.php', 'customize.php');
}
add_action('admin_init', 'remove_customize', 999);
一旦我更新到4.0,它就不再工作了。
发布于 2017-01-06 05:34:33
编辑:更新了WordPress 4.9+并增加了与PHP5.4的兼容性
WordPress核心不为本机禁用主题自定义程序提供钩子,但是有一种巧妙而优雅的方法可以通过更改全局$submenu
变量从外观菜单中删除“自定义”链接:
/**
* Remove Admin Menu Link to Theme Customizer
*/
add_action( 'admin_menu', function () {
global $submenu;
if ( isset( $submenu[ 'themes.php' ] ) ) {
foreach ( $submenu[ 'themes.php' ] as $index => $menu_item ) {
if ( in_array( array( 'Customize', 'Customizer', 'customize' ), $menu_item ) ) {
unset( $submenu[ 'themes.php' ][ $index ] );
}
}
}
});
虽然这里和其他地方的其他代码示例不负责任地依赖全局$submenu变量的特定数值索引(例如$submenu['themes.php'][6][0]
,.),但该方法明智地遍历层次结构,因此它应该与较早版本(3.x)和较新版本的WordPress (4.x)兼容。
发布于 2014-11-11 19:57:39
这适用于WordPress 4.1、4.0和3.x:
编辑:根据WordPress 4.1兼容性进行调整:
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 );
发布于 2018-06-18 15:19:18
答案应是:
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 ] );
}
}
}
}
});
rjb在接受的答案中使用数组作为in_array()中的指针的方式不起作用。看看为什么是在医生里。我用另一个foreach替换了in_array,它循环遍历$menu_item数组,并寻找“定制”作为值的一部分。
用WordPress 4.9.6为我工作
https://stackoverflow.com/questions/25788511
复制相似问题