WordPress Admin Bar Overlapping Twitter Bootstrap Navigation [closed]

How to prevent the WordPress admin bar from overlapping with your Twitter Bootstrap navigation bar.

In response to: WordPress admin bar overlapping twitter bootstrap navigation

Asked by: @TheWebs

If you are using Twitter Bootstrap with WordPress and have an issue with the WordPress admin bar overlapping with your navigation bar, you’re probably pretty frustrated with some of these answers. I looked for solutions everywhere before ultimately deciding to just down-shift to a lower gear and figure out a solution that does exactly what I need it to do.

So here is an answer that doesn’t hide the WordPress admin bar, or move the WordPress admin bar to the bottom of your pages. This answer will keep the WordPress admin bar right where it belongs… At the top of your pages.

Please note this will take a few short steps to complete, but it’s worth it. It’s not really a complicated or time consuming process. I just wanted to make sure the explanation on how to do so was clear and easy to follow/understand.


Step 1

Themes have a template tag for the body tag which will help theme
authors to style more effectively with CSS. The Template Tag is called
body_class. This function gives the body element different classes and
can be added, typically, in the header.php‘s HTML body tag.

  1. Open your currently active WordPress theme using Twitter Bootstrap
    directory. Find header.php and open it.
  2. Find <body>.
  3. Replace with <?php echo '<body class="'.join(' ', get_body_class()).'">'.PHP_EOL; ?>

After completing the three steps above, you have now successfully enabled your WordPress theme with WordPress body classes.

Step 2 (OPTIONAL!)

  • Add custom conditional CSS classes to the <body> tag.

By default, WordPress already provides a list of default classes to the HTML tag, if you are using the body_class() or get_body_class() functions.

If you view the source code of any rendered front-end page on your WordPress website, you will notice two of the CSS classes automatically added to the HTML <body> tag are “logged-in” and “admin-bar”.

You will also notice those CSS class names are only added to the HTML <body> tag if the user is logged in, otherwise they won’t be added to the HTML <body> tag.

So if you don’t want to use the default WordPress CSS class names, you can add your own very easily.

  1. Open your currently active WordPress theme using Twitter Bootstrap directory. Find functions.php and open it.
  2. Add add_filter('body_class', 'mbe_body_class'); to the top of the
    file.
  3. Add the following code, to the bottom of the file.

The code:

function mbe_body_class($classes){
    if(is_user_logged_in()){
        $classes[] = 'body-logged-in';
    } else{
        $classes[] = 'body-logged-out';
    }
    return $classes;
}

Now, if you view the source code of any rendered front-end page on your WordPress website, if the user is logged in, you will see “body-logged-in” has been added to the HTML <body> tag, and if the user is logged out, you will see “body-logged-out” has been added to the HTML <body> tag.

Step 3

  • Add the CSS code to your theme.

This is the section of code which will correct your theme to display both the WordPress admin bar, and your Twitter Bootstrap navigation properly, whether the user is logged in or logged out of your website.

  1. Open your currently active WordPress theme using Twitter Bootstrap directory. Find functions.php and open it.
  2. Add add_action('wp_head', 'mbe_wp_head'); to the top of the file.
  3. Add the following code, to the bottom of the file.

The code:

function mbe_wp_head(){
    echo '<style>'.PHP_EOL;
    echo 'body{ padding-top: 70px !important; }'.PHP_EOL;
    // Using custom CSS class name.
    echo 'body.body-logged-in .navbar-fixed-top{ top: 28px !important; }'.PHP_EOL;
    // Using WordPress default CSS class name.
    echo 'body.logged-in .navbar-fixed-top{ top: 28px !important; }'.PHP_EOL;
    echo '</style>'.PHP_EOL;
}

EDIT TO CODE

add_action('wp_head', 'mbe_wp_head');
function mbe_wp_head(){
    echo '<style>'
    .PHP_EOL
    .'body{ padding-top: 70px !important; }'
    .PHP_EOL
    .'body.body-logged-in .navbar-fixed-top{ top: 46px !important; }'
    .PHP_EOL
    .'body.logged-in .navbar-fixed-top{ top: 46px !important; }'
    .PHP_EOL
    .'@media only screen and (min-width: 783px) {'
    .PHP_EOL
    .'body{ padding-top: 70px !important; }'
    .PHP_EOL
    .'body.body-logged-in .navbar-fixed-top{ top: 28px !important; }'
    .PHP_EOL
    .'body.logged-in .navbar-fixed-top{ top: 28px !important; }'
    .PHP_EOL
    .'}</style>'
    .PHP_EOL;
}

This version of the mbe_wp_head function includes a mobile-first media query, to ensure that your header is pushed down the proper distance. For mobile, the WP admin bar is 48px tall. After the 783px breakpoint, the admin bar shortens to only 28px tall.

There you have it. If the user is logged in, you should now have the WordPress admin bar at the very top of your page, immediately followed by your Twitter Bootstrapped navigation. If the user is logged out of your WordPress website, your Twitter Bootstrap navigation should still display appropriately at the top of your website.

Leave a Comment