Wednesday, 4 September 2013

How To Remove WordPress Admin Bar Without Plugin

Important: All scripts hosted on widcraft.googlecode.com don't work anymore because Google has blocked that SVN repository.

A blogger friend asked me about removing WordPress' admin bar without any plugin because he wanted to get rid of all small plugins from his directory. So I found this amazing php code on dfactory.eu.
Put any of the following codes in your Theme's function.php file:

Disable Admin Bar For Everyone:

// Disable Admin Bar for everyone
if (!function_exists('df_disable_admin_bar')) {

    function df_disable_admin_bar() {
       
        // for the admin page
        remove_action('admin_footer', 'wp_admin_bar_render', 1000);
        // for the front-end
        remove_action('wp_footer', 'wp_admin_bar_render', 1000);
         
        // css override for the admin page
        function remove_admin_bar_style_backend() {
            echo '<style>body.admin-bar #wpcontent, body.admin-bar #adminmenu { padding-top: 0px !important; }</style>';
        }     
        add_filter('admin_head','remove_admin_bar_style_backend');
       
        // css override for the frontend
        function remove_admin_bar_style_frontend() {
            echo '<style type="text/css" media="screen">
            html { margin-top: 0px !important; }
            * html body { margin-top: 0px !important; }
            </style>';
        }
        add_filter('wp_head','remove_admin_bar_style_frontend', 99);
      }
}
add_action('init','df_disable_admin_bar');

Disable Admin Bar For Everyone But Administrators:

// Disable Admin Bar for everyone but administrators
if (!function_exists('df_disable_admin_bar')) {

    function df_disable_admin_bar() {
       
        if (!current_user_can('manage_options')) {
       
            // for the admin page
            remove_action('admin_footer', 'wp_admin_bar_render', 1000);
            // for the front-end
            remove_action('wp_footer', 'wp_admin_bar_render', 1000);
           
            // css override for the admin page
            function remove_admin_bar_style_backend() {
                echo '<style>body.admin-bar #wpcontent, body.admin-bar #adminmenu { padding-top: 0px !important; }</style>';
            }     
            add_filter('admin_head','remove_admin_bar_style_backend');
           
            // css override for the frontend
            function remove_admin_bar_style_frontend() {
                echo '<style type="text/css" media="screen">
                html { margin-top: 0px !important; }
                * html body { margin-top: 0px !important; }
                </style>';
            }
            add_filter('wp_head','remove_admin_bar_style_frontend', 99);
           
        }
      }
}
add_action('init','df_disable_admin_bar');

Disable Admin Bar For Specific Users:

// Disable Admin Bar for specific user
if (!function_exists('df_disable_admin_bar')) {

    function df_disable_admin_bar() {
       
        // we're getting current user ID
        $user = get_current_user_id();
       
        // and removeing admin bar for user with ID 123
        if ($user == 123) {
       
            // for the admin page
            remove_action('admin_footer', 'wp_admin_bar_render', 1000);
            // for the front-end
            remove_action('wp_footer', 'wp_admin_bar_render', 1000);
           
            // css override for the admin page
            function remove_admin_bar_style_backend() {
                echo '<style>body.admin-bar #wpcontent, body.admin-bar #adminmenu { padding-top: 0px !important; }</style>';
            }     
            add_filter('admin_head','remove_admin_bar_style_backend');
           
            // css override for the frontend
            function remove_admin_bar_style_frontend() {
                echo '<style type="text/css" media="screen">
                html { margin-top: 0px !important; }
                * html body { margin-top: 0px !important; }
                </style>';
            }
            add_filter('wp_head','remove_admin_bar_style_frontend', 99);
           
        }
      }
}
add_action('init','df_disable_admin_bar');

That's it!
Important: Check our new website TricksPanda.com for WordPress tutorials, plugins and more.
 
Powered by Blogger.