0

Is it possible to add custom css to a stylesheet in a theme depending on whether the user is logged in or not?

When I log in, the theme navigation and when viewing the main site bar is hidden behind the wpadminbar and I would like to change the css of the theme nav bar only when an admin is logged in and when the site is being viewed?

0

It depends on how your main site bar is being blocked.

you could just add some custom CSS to the

#wpadminbar {
    position: absolute;
    top: -xx px;
}

Where xx px moves the bar enough to see what you need to see.

0

To check whether a user is an admin, use ( current_user_can( 'manage_options' ) ).

Then you can enqueue your css file that changes the nav position accordingly in your functions.php:

add_action ( 'wp_enqueue_scripts', 'ic_admin_navbar_frontend_css' );
function ic_admin_navbar_frontend_css() {
    if( current_user_can( 'manage_options' ) && ! is_admin() ) { // we don't want to load this on the admin side, just the front end
        wp_enqueue_style( 'ic-navbar-fix', get_stylesheet_directory_uri() . '/pathtofile/in/templatedirectory/my_special.css' );
    } // end check for admin user on frontend
} // end ic_navbar function

Hope that helps.

EDIT: Sorry, I misread the question... To check whether someone is logged in, admin or not, you can use is_user_logged_in(). Just replace current_user_can() above and you should be good to go.

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.