Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make wordpress admin menu collapsed by default?

I hope you all have seen wordpress admin panel. We have option at the end of menu to collapse or expand the menu.

If you click on collapse the menu gets collapsed and that settings gets saved ( I don't know where ) but if you login again you will see the same collapsed menu..

Where are they storing that data ??? I want to make admin menu to be shown collapsed by default how do i do that ?

Edit : I think the file wp-admin/js/common.js is responsible for that.. you can view the file here http://phpcrossref.com/xref/wordpress/wp-admin/js/common.js.txt

I think I got the code which is responsible for that, but I am new to js. The code is as follows :

$('#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 } );
});
like image 463
VIVEK SEDANI Avatar asked Jan 08 '23 21:01

VIVEK SEDANI


1 Answers

Finally I found the answer :

We just need to add class 'folded' in body tag to make admin menu folded. I added the class in body tag using JavaScript : document.body.className+=' folded';

here is the complete code that I added to the functions.php ( you can add that to your plugin also )

the code :=

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

}
add_action('admin_footer', 'custom_admin_js');

and it worked :)

like image 98
VIVEK SEDANI Avatar answered Jan 16 '23 02:01

VIVEK SEDANI