是否可以限制管理面板中的最大菜单深度/nav-menus.php
?
我需要这个,因为我的主题不支持两个以上级别的菜单,所以允许用户创建更深层次的菜单是没有意义的。
是否可以限制管理面板中的最大菜单深度/nav-menus.php
?
我需要这个,因为我的主题不支持两个以上级别的菜单,所以允许用户创建更深层次的菜单是没有意义的。
跟进@jmarceli和@squarecandy的精彩答案。以下是一个解决方案,它允许:
更容易缩放(在php操作中设置对象)
/**
* Limit max menu depth in admin panel
*/
function limit_admin_menu_depth($hook)
{
if ($hook != \'nav-menus.php\') return;
wp_register_script(\'limit-admin-menu-depth\', get_stylesheet_directory_uri() . \'/js/admin/limit-menu-depth.js\', array(), \'1.0.0\', true);
wp_localize_script(
\'limit-admin-menu-depth\',
\'myMenuDepths\',
array(
\'primary\' => 1, // <-- Set your menu location and max depth here
\'footer\' => 0 // <-- Add as many as you like
)
);
wp_enqueue_script(\'limit-admin-menu-depth\');
}
add_action( \'admin_enqueue_scripts\', \'limit_admin_menu_depth\' );
js公司/**
* Limit max menu depth in admin panel
* Expects wp_localize_script to have set an object of menu locations
* in the shape of { location: maxDepth, location2: maxDepth2 }
* e.g var menu-depths = {"primary":"1","footer":"0"};
*/
(function ($) {
// Get initial maximum menu depth, so that we may restore to it later.
var initialMaxDepth = wpNavMenu.options.globalMaxDepth;
function setMaxDepth() {
// Loop through each of the menu locations
$.each(myMenuDepths, function (location, maxDepth) {
if (
$(\'#locations-\' + location).prop(\'checked\') &&
maxDepth < wpNavMenu.options.globalMaxDepth
) {
// If this menu location is checked
// and if the max depth for this location is less than the current globalMaxDepth
// Then set globalMaxDepth to the max depth for this location
wpNavMenu.options.globalMaxDepth = maxDepth;
}
});
}
// Run the function once on document ready
setMaxDepth();
// Re-run the function if the menu location checkboxes are changed
$(\'.menu-theme-locations input\').on(\'change\', function () {
wpNavMenu.options.globalMaxDepth = initialMaxDepth;
setMaxDepth();
});
})(jQuery);
我想到的解决方案是:
/**
* Limit max menu depth in admin panel to 2
*/
function q242068_limit_depth( $hook ) {
if ( $hook != \'nav-menus.php\' ) return;
// override default value right after \'nav-menu\' JS
wp_add_inline_script( \'nav-menu\', \'wpNavMenu.options.globalMaxDepth = 1;\', \'after\' );
}
add_action( \'admin_enqueue_scripts\', \'q242068_limit_depth\' );
跟进@jmarceli对特定菜单位置的出色回答
function squarecandy_menu_admin_limit_depth( $hook ) {
if ( \'nav-menus.php\' !== $hook ) {
return;
}
wp_enqueue_script( \'squarecandy-admin-menu-depth\', get_stylesheet_directory_uri() . \'/js/admin-menu-depth.js\', array(), \'1.0.0\', true );
}
add_action( \'admin_enqueue_scripts\', \'squarecandy_menu_admin_limit_depth\' );
js公司// Force Menu Depth to Match Front End
// main menu can only have one child level
if ( document.getElementById("locations-main-menu").checked ) {
wpNavMenu.options.globalMaxDepth = 1;
}
// footer menu have only top level
if ( document.getElementById("locations-footer-menu").checked ) {
wpNavMenu.options.globalMaxDepth = 0;
}
// all other menu locations stay at the WP default
我是一个自学成才的wordpress网站设计师,请原谅我的无知。我为一个客户开发的几个网站最近被一个印尼黑客组织破坏了(很有趣!)。客户端没有更新wordpress安装和其他一些危及安全的事情。遗憾的是,没有网站的清理备份。网络主机给了我一个包含恶意软件扫描结果的文本文件。看起来很多受影响的文件都是导航菜单。网站的php文件。文本文件中显示的扫描结果示例如下:“[主目录]/[网站文件夹名称]/wp includes/nav-menu.php:SL-php-INJECTOR-1-ejw.UNOFFICIAL