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

VIV*_*ANI 4 javascript css wordpress jquery

我希望你们都看过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 } );
});
Run Code Online (Sandbox Code Playgroud)

VIV*_*ANI 8

最后我找到了答案:

我们只需要在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');
Run Code Online (Sandbox Code Playgroud)

它工作:)


Chi*_*lan 8

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

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

add_filter("admin_body_class", "my_folded_menu", 10, 1);

function my_folded_menu($classes){
    return $classes." folded";
}
Run Code Online (Sandbox Code Playgroud)

它的作用是一样的,它增加了档次。只是它是从后端而不是前端执行的。

  • 杰出的!这应该是公认的答案。 (2认同)