Support » Fixing WordPress » Admin bar default color scheme for nonregistered/nonlogged users

  • Resolved kr3t3n


    Hi guys, happy holidays!

    I’m looking for some help customizing the toolbar, which I’ve forced to be visible at all times, even for non-registered/non-logged users.

    I have disabled the registered users to select the admin color scheme as I want all of them to use the ‘Coffee’ scheme. I have also made the ‘Coffee’ color scheme the default one for registered users.

    However, WordPress still shows the default (black) admin bar for nonregistered/nonlogged users of the website.

    Do you know how I can force it show the admin bar in the ‘Coffee’ color scheme even in those cases?

    Thank you very much in advance.

Viewing 12 replies - 1 through 12 (of 12 total)
  • Moderator bcworkz


    Hook the ‘admin_print_styles’ action and conditionally output (when get_current_user_id() returns 0) an inline style block that would override whatever CSS rules you want to change. Hook with a large priority argument so your style has the final say in appearance.

    Hi bcworkz, happy new year!

    Thank you for the response. My knowledge is quite limited and I’m unsure how to do what you are suggesting. Can you please help me out? Where should I write that piece of code and how?

    I appreciate your help!

    Moderator bcworkz


    Learn about action hooks in the Plugin Handbook. Your callback function simply echoes out CSS styles only when the user ID is zero. The first action example is the basic framework, replace “init’ with โ€˜admin_print_stylesโ€™ and conditionally echo CSS in place of // do something.

    This code can go in the functions.php file of your theme, but it will be overwritten when the theme updates. To prevent this, implement a child theme. Another good place for custom code is your own site specific plugin (easier to setup than it sounds). If your customizations involve templates, a child theme is better. Plugins can also handle templates, but doing so is rather tricky.

    It may seem like overkill to create a child theme or plugin for one simple action hook, but any other custom code you may want to add in the future can go in the same place, so it’s a good thing to have setup from the start.

    Thank you for the pointers. I am already using a child theme and have quite a few custom snippets of code in the functions.php (copied and adjusted for different reasons).

    Since I want to load the complete ‘Coffee’ color scheme, I am thinking that I need to somehow reference the complete .css file, maybe even all 5 of them (wp-admin/css/colors/coffee/colors.css / colors.min.css / colors.scss / colors-rtl.css / colors-rtl.min.css), however I’m really not sure how to do that.

    I guess it would have to be something like:

    function admin_bar_coffee_forced()
    	echo '<link rel="stylesheet" href="" type="text/css">';
    add_action('admin_print_styles', 'admin_bar_coffee_forced');

    But this is definitely not working. Can you point where I’m going wrong?

    Thank you very much for your time and help!

    ( is the domain of the website where I’m trying to force the ‘Coffee’ color scheme for the admin bar)

    • This reply was modified 1 year, 1 month ago by kr3t3n. Reason: adding explanation on the domain name
    Moderator bcworkz


    That is very close to what I had in mind. Nicely done even if it doesn’t work ๐Ÿ™‚ There’s an easy fix to get it working. If you check the admin page HTML source, you’ll see your link OK, but it appears before the default color link, so it cannot override the CSS. To get it to appear later, you need to add a large priority argument to your add_filter() call (third parameter). The default is 10, so 20 ought to do it, though you could put in a larger value to be safe. The upper limit is determined by the PHP max integer limit on your system, something really huge. 99 is probably larger than most parameters used. You could pass the constant PHP_INT_MAX to really be sure ๐Ÿ™‚

    I also encourage the use of admin_url() instead of hardcoding your full URL. Then if you ever have to move your site (even just to a subfolder), you will not have hidden URLs like this in code messing up the move. WP will adjust automatically.

    Thanks bcworkz, very valuable tips! ๐Ÿ™‚

    However, you mention that the link will be visible in my admin page. My intention is for this override to be visible on the front end. I have already forced the admin bar to appear on the front end for unregistered / not logged users, but it is only the black template for them. Logged users already see the ‘Coffee’ color scheme, so that’s fine for them.

    I noticed that with that code the link does not appear at all on the front end. Is there something I’m missing there?

    Moderator bcworkz


    Ah, right, I didn’t fully think my suggestions through. Apologies. Add another action hook for the “wp_print_scripts” action. Same callback, just add another add_action() line.

    Awesome, thank you very much, @bcworks!

    It works even without the priority parameter!

    One more question!

    In order to use admin_url, how would I make it work for <link rel=”stylesheet” href=”css/colors/coffee/colors.css” type=”text/css”> for the echo?

    Moderator bcworkz


    With concatenation. Like this:
    echo '<link rel="stylesheet" href="' . admin_url('css/colors/coffee/colors.css') . '" type="text/css">';

    If you are keen to implement better code, you may notice the colors.css link will appear on front end pages even when the user is not logged in and thus no admin bar exists to style. There is little harm from this other than it’s a bit less efficient. To prevent unnecessary links from appearing, conditionally echo only when the user is logged in.
    if ( is_user_logged_in()) echo 'HTML link tag goes here';

    Truth be told, using the print styles hooks is rather hacky to start with. There is a more involved system of organizing stylesheets that makes use of calls to wp_enqueue_style(). The end result is the same. The advantage is the dependencies are resolved automatically so we don’t have to manually fuss with priorities. I didn’t want to overwhelm you with all of this for simply adding a link tag, but maybe you should at least know that directly using print styles hooks are not entirely “proper”.

    It’s an issue for plugins and themes distributed to others because conflicts can easily develop. For your own site you’re free to do whatever gets the job done.

    Thank you very much, @bcworks, I get it now (I think ๐Ÿ˜€ )!

    Since I am forcing the admin bar to be displayed for users who are not logged in, I will skip the ‘if’ statement. I’ve tested it and it works perfectly!

    Moderator bcworkz


    Ah, right, skip that. Your not logged in detail escaped me.

    You’re welcome. Glad I could help, even if a bit confused at times.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Admin bar default color scheme for nonregistered/nonlogged users’ is closed to new replies.