如何使wordpress管理菜单默认折叠

How to make wordpress admin menu collapsed by default?

本文关键字:默认 折叠 菜单 管理 何使 wordpress      更新时间:2024-01-09

我希望你们都看过wordpress管理面板。我们可以在菜单末尾选择折叠或展开菜单。

如果你点击折叠,菜单会被折叠,设置会被保存(我不知道在哪里),但如果你再次登录,你会看到同样的折叠菜单。。

他们把这些数据存储在哪里???我想让管理菜单显示默认折叠,我该怎么做?

编辑:我认为wp-admin/js/common.js文件对此负责。。你可以在这里查看文件http://phpcrossref.com/xref/wordpress/wp-admin/js/common.js.txt

我想我已经得到了负责这一点的代码,但我是js的新手。代码如下:

$('#collapse-menu').on('click.collapse-menu', function() {
    var body = $( document.body ), respWidth, state;
    // reset any compensation for submenus near the bottom of the screen
    $('#adminmenu div.wp-submenu').css('margin-top', '');
    if ( window.innerWidth ) {
        // window.innerWidth is affected by zooming on phones
        respWidth = Math.max( window.innerWidth, document.documentElement.clientWidth );
    } else {
        // IE < 9 doesn't support @media CSS rules
        respWidth = 961;
    }
    if ( respWidth && respWidth < 960 ) {
        if ( body.hasClass('auto-fold') ) {
            body.removeClass('auto-fold').removeClass('folded');
            setUserSetting('unfold', 1);
            setUserSetting('mfold', 'o');
            state = 'open';
        } else {
            body.addClass('auto-fold');
            setUserSetting('unfold', 0);
            state = 'folded';
        }
    } else {
        if ( body.hasClass('folded') ) {
            body.removeClass('folded');
            setUserSetting('mfold', 'o');
            state = 'open';
        } else {
            body.addClass('folded');
            setUserSetting('mfold', 'f');
            state = 'folded';
        }
    }
    $( document ).trigger( 'wp-collapse-menu', { state: state } );
});

最后我找到了答案:

我们只需要在body标签中添加类"折叠",使管理菜单折叠。我使用JavaScript在body标记中添加了类:document.body.className+='folded';

这是我添加到functions.php的完整代码(你也可以将其添加到你的插件中)

代码:=

function custom_admin_js() {
    echo "<script type='text/javascript' > 
document.body.className+=' folded';                 
</script>";
}
add_action('admin_footer', 'custom_admin_js');

它起作用了:)

上面的解决方案是一个很好的方法。但在这个解决方案中,我们可以看到一些闪烁,直到页面加载并执行脚本(如果它被绘制),我们才能看到它打开。

在深入研究了一些核心文件后,我通过后端提出了这个解决方案。

add_filter("admin_body_class", "my_folded_menu", 10, 1);
function my_folded_menu($classes){
    return $classes." folded";
}

这是一样的,它增加了类。只是它从后端而不是前端完成。

您需要找到模板使用auto-foldfolded类的位置,并确保页面加载时默认为folded。我不能说更多,直到你发现在哪里完成。