2

I am trying to hide WordPress admin bar from all pages except the home page.

I added the following CODE in the theme's functions.php file, but it hides the admin bar on all pages:

if ( ! is_single() ) {
   add_filter( 'show_admin_bar', '__return_false' );
}

I want it to appear only on the homepage and to be hidden everywhere else.

1

You should use conditional tag inside function hooked to show_admin_bar filter.

add_filter( 'show_admin_bar', 'show_adminbar_on_homepage_only', 20 );
function show_adminbar_on_homepage_only()
{
     return is_user_logged_in() && is_front_page();
}
  • this does the trick to show the admin bar on the homepage only, however, when i log out the bar box is stil visible on the homepage, though there is no user data on it anymore – Badan May 24 at 10:22
  • @Badan It should work properly now. – nmr May 24 at 10:25
  • now it is hidden everywhere again. – Badan May 24 at 10:34
  • I removed the is_home() check and now it is working like a charm! But i do not undestand how it works. Can you provide brief explanation? – Badan May 24 at 10:39
  • The show_admin_bar filter hook allows you to change display status of the admin bar for front side, if function attached to this hook returns TRUE, admin bar will be visible. Conditional tags is_home() and is_front_page() (similar, but slightly different, here you can read more about them) check whether the currently displayed page is the home page, and the is_user_logged_in() - as the name suggests. It's strange that you have to remove is_home(), I checked and is_user_logged_in() && (is_home() || is_front_page()) works for me. – nmr May 24 at 12:31
0

Could be a priority issue, meaning that some plugin / theme function is overriding the admin bar behaviour. Try something like this:

if (!is_home() && !is_front_page()){
   add_filter('show_admin_bar', '__return_false', 999);
}

If this work, let's try to understand which plugins are and assign the right priority, like a lower 99 or whatever.

  • i tried with the priority but it is still not working.. maybe i should put the code in a different file, not in functions.php? – Badan May 24 at 10:04
  • let's try this, put in functions.php the raw function show_admin_bar(false); and let me know. – huraji May 24 at 10:07
  • i put show_admin_bar(false); it hides the admin bar everywhere. – Badan May 24 at 10:15
  • Let's try now with just the add_filter('show_admin_bar', '__return_false'); without conditionals and let me know. – huraji May 24 at 10:17
  • both options work for all pages – Badan May 24 at 10:24
0

Here's a better way to do it that improves on the existing answers:

add_filter( 'show_admin_bar', function ( $show ) {

    if ( is_front_page() ) {
        return false;
    }

    return $show;
}, 50 );

Unlike the other code examples given, this snippet will only change the admin bar visibility if currently viewing the front page. Otherwise, it will use whatever the default is set to. This removes the need to account for different cases where the user is not logged in.

-1

You should check if page is home page and/or front page. is_front_page() returns true when viewing the site's front page (blog posts index or a static page), is_home() returns true when viewing the blog posts index. try as below,

if (!is_home() && !is_front_page()){
   add_filter('show_admin_bar', '__return_false');
}

or

if (!is_home()){
   add_filter('show_admin_bar', '__return_false');
}
  • i tried this but it is also not working.. – Badan May 24 at 9:10
  • try with else (is_home()){ add_filter('show_admin_bar', '__return_true'); } – Vishwa May 24 at 9:25
  • still hiding it everywhere.. – Badan May 24 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.