4

I have tried with add_filter('show_admin_bar', '__return_false'); in wordpress 4.8 version and its not hiding the admin bar.

Help me with a solution.

andreas
  • 16,357
  • 12
  • 72
  • 76
priya
  • 158
  • 5
  • 16

2 Answers2

16

Try add_filter( 'show_admin_bar', '__return_false', PHP_INT_MAX );

Dennis Zeng
  • 161
  • 1
  • 4
  • What does`PHP_INT_MAX` means? and why does `add_filter('show_admin_bar', '__return_false');` doesn't work? – Codeblooded Saiyan Aug 13 '18 at 07:29
  • 2
    I think that the trick here si that the third parameter of `add_filter`is supposed to be an int representing the priority of the filter. From the doc : `$priority (int) (Optional) Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.` So using PHP_INT_MAX means using the maximum numeric value avaiable. 9999 would have made the trick. – Gfra54 Dec 20 '18 at 10:54
  • Why is `show_admin_bar(false)` not working? According to https://developer.wordpress.org/reference/functions/show_admin_bar/ this function should work if I simply put it in the `function.php` – shenkwen Apr 08 '22 at 18:16
6

Try this to remove admin bar for all roles except administrator

function remove_admin_bar()
{
    if (current_user_can('administrator')) {
        return true;
    }
    return false;
}

add_filter('show_admin_bar', 'remove_admin_bar', PHP_INT_MAX);
Steven
  • 1,996
  • 3
  • 22
  • 33
Ibtihaaj G
  • 61
  • 1
  • 2