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..

Read More

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 } );
});

Related posts

Leave a Reply

3 comments

  1. 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 🙂

  2. The above solution is a good way to do. But in this solution, we could get to see some flashes, where until the page is loaded and script executes if it is painted, then we might be able to see it open.

    I came up with this solution via backend after digging into some core files.

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

    What this does is the same thing, it adds class. Just that it does it from the backend instead of the frontend.

  3. You’ll need to find where the template uses the auto-fold and folded classes, and make sure that the default is folded on page load. I can’t say more until you find out where that is done.