Categories: Wordpress Tricks

How to hide/disable admin bar from WordPress

Sometimes we are not required to show admin bar from the front side of WP sites which likes odd if not necessary for all types of users. So, here are few tricks & functions which will allow you to do this:

 

FORMULA-1 : Hook Function which you have to paste in functions.php file

# If want to hide for all types of users including administrators:

add_action('after_setup_theme', 'remove_admin_bar');

function remove_admin_bar() {
show_admin_bar( false );
}


# If want to hide all types of users except administrators:

add_action('after_setup_theme', 'remove_admin_bar');

function remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
show_admin_bar( false );
}
}

# Hide admin bar from subscribers:

add_action('set_current_user', 'cc_hide_admin_bar');
function cc_hide_admin_bar() {
  if (!current_user_can('edit_posts')) {
    show_admin_bar(false);
  }
}

FORMULA-2: ADD CSS & HOOK CSS CODE TO FUNCTION

#simply you can add this css code to your CSS file:

html { margin-top: 0px ; }
* html body { margin-top: 0px ; }
<span class="cssSelector editable ">#wpadminbar</span> {display:none ;}

# hook action to functions.php file for adding css:

function remove_admin_bar_css() {
    echo '<style type="text/css" media="screen">
    html { margin-top: 0px ; }
    * html body { margin-top: 0px ; }
    </style>';
}
add_filter('wp_head','remove_admin_bar_css, 99);

I hope you can find your suitable solution from the above and fulfill your requirements. Thanks.

This post has already been read 3381 times!

Mehedi Hasan

Cool WordPress Developer having much agile experience to develop any kind of WordPress sites & plugins. Also good in troubleshooting, fixing & making any kind of tweaks for WP site.

More Posts

Mehedi Hasan

Cool WordPress Developer having much agile experience to develop any kind of WordPress sites & plugins. Also good in troubleshooting, fixing & making any kind of tweaks for WP site.

Share
Published by
Mehedi Hasan
Tags: admin

Recent Posts

WP Share A Friend – A Way to Capture Lead

What is Lead Capture: In general sense lead capture designates a process or way of…

5 years ago

Unlimited Modal POPUPS on MouseClick

Introduction Unlimited Modal POPUPS on MouseClick: Everyone knows about popup during site visits and most…

5 years ago

WordPress Pregnancy Calculator Plugin

Brief about Pregnancy Calculator : This WordPress Pregnancy Calculator plugin allows users to display an…

5 years ago

Interactive Service Plugin with Hover Effects VC

Introduction About this Service Plugin: This Visual Composer Service Plugin works as an addon to…

6 years ago

WP Dynamic Query String

Introduction of Dynamic Query String : This plugin allows user to set dynamic query string…

6 years ago

How to make scroll PDF in iFrame for iPAD Safari

There is an occasional bug for iPhone and iPad even they are designed as well and…

7 years ago