Make Custom Admin Bar settings be applied only for regular users, not for admins

Hi!

I'm using the version of Custom Admin Bar that comes with Ultimate Branding and I want the changes I make to be applied only for regular users, not for the admins -regular users are registered as subscribers, btw. How do I do that?

Thanks.

  • Wheel of Commerce

    Thanks for answering.

    What I want is that super-admins and admins see the standard wordpress admin bar (the way it was before customizing it with Custom Admin Bar) and for the regular users to see the wp admin bar the way it's set by Custom Admin Bar. For instance: the My Sites button that has a key icon beside it, that shouldn't be seen by regular users.

    If this is something that takes too much work to get a code customized, I'd just like to know how can I, as super-admin, see the My Sites list, or at least an equivalent of that.

    Thanks again.

  • Wheel of Commerce

    Updating with what discussed during chat with Tyler Postle:
    I've addd the following inside my functions.php:

    function wpa66834_role_admin_body_class( $classes ) {
        global $current_user;
        foreach( $current_user->roles as $role )
            $classes .= ' role-' . $role;
        return trim( $classes );
    }
    add_filter( 'admin_body_class', 'wpa66834_role_admin_body_class' );

    And the following inside the CSS panel of Custom Admin Bar:

    .role-subscriber .wp-admin-bar-my-sites {
    display: none;
    }

    It didn't work.

  • Wheel of Commerce

    Hi, man! Just tried it, didn't work too. In case you need, support access is active and I've sent you guys my FTP login data with Michelle Schull's name in the subject line a couple of days ago.

    Not sure if it can have something to do with it, but the "Reorder Menus" function of Custom Admin Bar isn't working either -after saving the changes made, everything gets back to the way it was.

    Thanks again.

  • Vinod Dalvi

    Hi Gabriel,

    Try using the following code int he functions.php file of your child theme, replacing previously provided code.

    function wpa66834_role_admin_body_class( $classes ) {
       global $current_user;
    
    if(in_array('administrator', $current_user->roles)
             $classes .= ' role-administrator';
    
       return trim( $classes );
    }
    add_filter( 'admin_body_class', 'wpa66834_role_admin_body_class' );

    Also use the following CSS code in the style.css file of your child theme.

    body.role-administrator #wp-admin-bar-ub_admin_bar_18294,
    body.role-administrator #wp-admin-bar-ub_admin_bar_18466,
    body.role-administrator #wp-admin-bar-ub_admin_bar_18469{
    display: none;
    }

    Best Regards,
    Vinod Dalvi

  • Wheel of Commerce

    Hi! Thanks for replying. That still doesn't work, as changes are being applied to me as well, and I'm the super-admin. Below is the content of my functions.php, from the child theme's folder, to see if I've placed the code correctly, just in case:

    <?php
    
    add_filter('show_admin_bar', '__return_true');
    
    function wpa66834_role_admin_body_class( $classes ) {
       global $current_user;
    
    if(in_array('administrator', $current_user->roles)
             $classes .= ' role-administrator';
    
       return trim( $classes );
    }
    add_filter( 'admin_body_class', 'wpa66834_role_admin_body_class' );
    
    /**
     * Function Name: front_end_login_fail.
     * Description: This redirects the failed login to the custom login page instead of default login page with a modified url
    **/
    add_action( 'wp_login_failed', 'front_end_login_fail' );
    function front_end_login_fail( $username ) {
    
    // Getting URL of the login page
    $referrer = $_SERVER['HTTP_REFERER'];
    // if there's a valid referrer, and it's not the default log-in screen
    if( !empty( $referrer ) && !strstr( $referrer,'wp-login' ) && !strstr( $referrer,'wp-admin' ) ) {
        wp_redirect( get_permalink( 66 ) . "?login=failed" );
        exit;
    }
    
    }
    
    /**
     * Function Name: check_username_password.
     * Description: This redirects to the custom login page if user name or password is   empty with a modified url
    **/
    add_action( 'authenticate', 'check_username_password', 1, 3);
    function check_username_password( $login, $username, $password ) {
    
    // Getting URL of the login page
    $referrer = $_SERVER['HTTP_REFERER'];
    
    // if there's a valid referrer, and it's not the default log-in screen
    if( !empty( $referrer ) && !strstr( $referrer,'wp-login' ) && !strstr( $referrer,'wp-admin' ) ) {
        if( $username == "" || $password == "" ){
            wp_redirect( get_permalink( 66 ) . "?login=empty" );
            exit;
        }
    }
    
    }
    // Replace my constant 'LOGIN_PAGE_ID' with your custom login page id.
    
    //custom load footer text
    	function custom_mpt_load_footer_text() {
    		$customfooter = get_option('mpt_cus_footer');
    		$custom = wp_kses( $customfooter, array(
    					'a' => array(
    						'href' => array(),
    						'title' => array()
    						),
    					'br' => array(),
    					'em' => array(),
    					'strong' => array()
    					) );
    		$date = date("Y");
    		switch_to_blog(1);
    		$siteurl = esc_attr(get_bloginfo('url'));
    		$sitename = esc_attr(get_bloginfo('name'));
    			if(!empty($custom)) {
    				echo apply_filters( 'func_custom_mpt_load_footer_text' , '<p>'.$custom.'</p><p class="alignright">'. __( 'Copyright &copy;' , 'pro' ) .$date.' <a href="'.$siteurl.'"> '.$sitename.' </a></p>' );
    			 } else {
    				echo apply_filters( 'func_custom_mpt_load_footer_text' , '<p>'. __( 'Copyright © 2014 - ' , 'pro' ) .$date.' <a href="'.$siteurl.'"> '.$sitename.' </a></p>' );
    
    			}
    	}

    Thanks again.

  • Vaughan

    Hi Gabriel,

    I spotted an error in the code above.

    Can you replace the following function.

    function wpa66834_role_admin_body_class( $classes ) {
       global $current_user;
    
    if(in_array('administrator', $current_user->roles)
             $classes .= ' role-administrator';
    
       return trim( $classes );
    }
    add_filter( 'admin_body_class', 'wpa66834_role_admin_body_class' );

    there's a missing ) in the if condition.

    function wpa66834_role_admin_body_class( $classes ) {
       global $current_user;
    
    if(in_array('administrator', $current_user->roles))
             $classes .= ' role-administrator';
    
       return trim( $classes );
    }
    add_filter( 'admin_body_class', 'wpa66834_role_admin_body_class' );

    Hope this helps

  • Vaughan

    Hi Gabriel,

    I've sorted the menu for you.

    Note.

    I had to add the CSS into the functions.php using admin_head filter, as for some reason no matter what, the wheel of commerce menu item just wouldn't disappear. But all is good now.

    Here's the amended code in your functions.php

    function wpa66834_role_admin_body_class( $classes ) {
       global $current_user;
    
    if(in_array('administrator', $current_user->roles))
             $classes .= ' role-administrator';
    
       return trim( $classes );
    }
    add_filter( 'admin_body_class', 'wpa66834_role_admin_body_class' );
    
    function wpmudev_role_custom_menu_css() {
    
        echo '<style>
        body.role-administrator #wp-admin-bar-ub_admin_bar_18294 {display: none !important;}
        body.role-administrator #wp-admin-bar-ub_admin_bar_18466 {display: none !important;}
        </style>';
    }
    
    add_filter('admin_init', 'wpmu_custom_css');
    function wpmu_custom_css() {
       global $current_user;
    
        if(in_array('administrator', $current_user->roles)) {
            add_filter( 'admin_head', 'wpmudev_role_custom_menu_css' );
        }
    }

    Hope this helps

  • Wheel of Commerce

    Hi, man! Thank you very much for putting your time into this, but I just tested it and the changes made in Custom Admin Bar are still being applied to me (super-admin), haha -not sure if I laugh or cry, lol. If for some reason this is something really hard to achieve, I'd like to at least have a way to have the My Sites button in the wp bar to be displayed only to me, the super-admin.

    Thanks again. :slight_smile:

  • Vinod Dalvi

    Hi Gabriel,

    It seems this lengthy topic led to confusing regarding what you want to display for regular users and for admin.

    Could you please share two screenshots displaying what you want to display for regular users and what for admins users?

    I think this way we can better understand it and get to the bottom of this faster.

    When you reply here, please make sure the support staff access is active on your site.

    Regards,
    Vinod Dalvi

  • Wheel of Commerce

    Hi and thanks for replying.

    I'll give an example to make things simple: in the Custom Admin Bar settings, when I select, for instance, My Sites to be hidden, it's getting hidden not only for regular users (subscribers), but for me, the super-admin, as well. What I want is for the super-admin to see the standard wp bar, and for the modifications made through Custom Admin Bar to be displayed only for the regular users.

    Thanks again.

  • Vaughan

    Hi,

    So, any changes made using custom admin bar should only be displayed to members and not admins at all?

    This makes it a little more tricky, I guess we could perhaps use a wildcard.

    changing;

    body.role-administrator #wp-admin-bar-ub_admin_bar_18294 {display: none !important;}

    to

    body.role-administrator li[id*=&quot;wp-admin-bar-ub_admin_bar&quot;] {display: none !important;}

    I made this change for you in the functions.php

    function wpmudev_role_custom_menu_css() {
    
        echo '<style>
        body.role-administrator ul li[id*=&quot;wp-admin-bar-ub_admin_bar_&quot;] {display: none !important;}
        </style>';
    }

    This removed the custom admin bar changes for the admin.

    But now i'm thinking this still may not be what you want. Let us know. If this isn't how you want it, it would certainly require some custom coding in the plugin I think which would not be as simple a task.

    Hope this helps

  • Wheel of Commerce

    Hi, man!

    "So, any changes made using custom admin bar should only be displayed to members and not admins at all?"
    Exactly. I've just tested and the changes are now partially still being applied to admins, as the menu items I've created are not being shown at my wp bar anymore, but any changes made in the Hide WordPress Menu Items part still are. Well, I guess this is 50% of the way, at least, so that's good news. :slight_smile:

    Something that just came to my mind: can this issue have anything to do with the fact that I'm using New Blog Templates? I honestly don't think it does, but I'm asking because, well, "better safe than sorry", haha.

    Thanks again.

  • Vaughan

    Hi Gabriel,

    There are no hooks/filters for this, so it will mean some custom code in the plugin.

    I have edited the plugin code.

    /ultimate-branding/ultimate-branding-files/modules/custom-admin-bar-files/inc/UB_Admin_Bar.php

    in function add_custom_menus

    if( !$enabled ) return;

    replaced with

    if( !$enabled || current_user_can('manage_options')) return;

    Then in function remove_menus_from_admin_bar()

    $version = preg_replace('/-.*$/', '', $wp_version);
                if (version_compare($version, '3.3', '>=')) {
                    global $wp_admin_bar;
                    $opts = ub_get_option('wdcab');
                    $disabled = is_array(@$opts['disabled_menus']) ? $opts['disabled_menus'] : array();
                    foreach ($disabled as $id) {
                        $wp_admin_bar->remove_node($id);
                    }
                }

    Replaced with;

    if(!current_user_can('manage_options')) {
                $version = preg_replace('/-.*$/', '', $wp_version);
                if (version_compare($version, '3.3', '>=')) {
                    global $wp_admin_bar;
                    $opts = ub_get_option('wdcab');
                    $disabled = is_array(@$opts['disabled_menus']) ? $opts['disabled_menus'] : array();
                    foreach ($disabled as $id) {
                        $wp_admin_bar->remove_node($id);
                    }
                }
            }

    Hopefully, if I understand you right, it should now be doing everything you wanted, any custom admin bar changes you make will no longer be applied to admin users.

    Please be aware, these changes will need to be re-applied everytime you update the plugin, however I will add this as a feature request to see if we can get some user_role options for the menu items so we can control which user roles can view the custom menus etc.

    Let me know if this is working as you require :slight_smile:

    Hope this helps

  • Wheel of Commerce

    Hi, man! Thank you very much for replying so fast.

    I checked, and it seems like now everything is working: all changes made through Custom Admin Bar are not being applied to the super-admin, just to regular users (subscribers). Regarding the modifications made in the code and the fact that I'll have to make them again every time the plugin gets an update, I was wondering if it isn't possible to create a plugin to apply these modifications in the code. I mean, I really have no idea if this would be difficult, as the last time I built a plugin it was for something very simple, and I pretty much just got a few pieces of code from different places together, so it wasn't like I had built it from scratch.

    Thank you very much again. :slight_smile:

  • Vaughan

    Hi,

    At this time it isn't possible as there are no hooks/filters in the module to be able to accomplish it, this is why I have had to add it as a feature request for the developer. Until the developer could add these filters in, it's the only way to achieve this.

    Bookmark this thread though, and make a note, if the plugin is updated, I actually don't mind to keep making this change for you if need be.

    Hope this helps

  • Wheel of Commerce

    Hi, man! Sorry for taking this long to answer, I've checked this issue and everything is working fine, I just forgot to come back here.

    If replacing those lines is all it takes, I can do it myself when the plugin gets an update, but thanks for offering help with it anyway. :slight_smile: And yes, it would be perfect to have it as a feature in the plugin, as I presume most admins would like to keep their admin bars as they are and have the changes applied only for regular users.

    I'm having another related issue though, it was still present before applying these changes to the code, though now I obviously can't see the menu items as admin anymore: I can't reorder the menu items. Considering this, it now came to my mind: since all I want is to have the wp menu items hidden from regular users, is it possible to insert a code that does just that, so I can still reorder the menu items as super-admin?

    Thanks again, man! I'll finally be able to have that wp bar displayed the way I've always wanted it for the users.

  • Vaughan

    Hi Gabriel,

    Yes, that's all it takes, those 2 edits :slight_smile:

    However, I have added this as a feature request. I have actually already coded this feature up so that users can select which user role can view each custom menu item & it's all working perfectly on my site. I just have to wait for the developer to review my code changes & if he approves it, it could likely be included in a future release.

    If you wish to make it so that they are only displayed to the super-admin, change the following line

    if(!current_user_can('manage_options')) {

    to

    if(!current_user_can('manage_network')) {

    Hope this helps

  • Wheel of Commerce

    Hi, and thanks for replying.

    I'm still not seeing the menu items I've inserted at my wp bar when I'm logged in as super-admin. Putting it as simple as possible: all I want is to hide the default wp menu items from regular users (subscribers), I still want to be able to see them as super-admin (which I already am), but I also want to see the menu items I've inserted, so I can change their order whenever it's necessary.

    Thanks again.

  • Vaughan

    Hi,

    To make it so that the default WP menus are only hidden to subscribers, change the following line;

    if(!current_user_can('manage_options')) {

    replace with;

    if(!current_user_can('edit_posts')) {

    For the custom menu items;

    Change the following line;

    if( !$enabled || current_user_can('manage_options')) return;

    replace with;

    if( !$enabled ) return;
    if(current_user_can('manage_network'))  {
    } elseif(current_user_can('edit_posts')) return;

    Hope this helps

  • Ash

    Hello @Gabriel

    I hope you are well today.

    Please replace

    if(!current_user_can('manage_options')) {

    with

    if(!current_user_can('manage_options') || defined('ENABLE_NETWORK_ADMIN_VIEW')) {

    And replace:

    if( !$enabled || current_user_can('manage_options')) return;

    with

    if( !$enabled || current_user_can('manage_options') || defined('ENABLE_NETWORK_ADMIN_VIEW')) return;

    Then when you want to make any modification, just add the following line in wp-config.php:

    define( 'ENABLE_NETWORK_ADMIN_VIEW', true );

    After making changes, remove the line again.

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

  • Wheel of Commerce

    Thanks for the answer, man!

    The thing is: after inserting that line in wp-config.php, it removes even the wp native menu items, haha. Maybe it's easier if I just state what I want in the simplest way possible: I want to hide wp menu items from regular users (subscribers) without having them being hidden from me (super-admin), that's all. The way it is right now, they're being hidden from non-admin users, but I can't see the custom menu items I've inserted, so I can't change their order -to do so, the only way right now is by deleting all of them, then creating it all again. As I'll have a reasonably complex menu at my site (it's for ecommerce), that's really not going to cut it.

    Thanks again, man! :slight_smile:

  • Ash

    Hello @Gabriel

    I want to hide wp menu items from regular users (subscribers) without having them being hidden from me (super-admin), that's all.

    That's already done by @Vaughan.

    The way it is right now, they're being hidden from non-admin users, but I can't see the custom menu items I've inserted, so I can't change their order -to do so

    Yes, that's logical. As you have made those menus for non-admins only, you won't see those as an admin.

    And it seems, normally you don't even need those menu to see, only non-admins need.

    But you need to edit the items sometimes, or reorder, right? In that case only, you will use that define. So, the define is not for always, you will use the define "only" when you want to edit admin bar. Once you are done with editing, remove that define.

    I am also requesting @Vaughan to test my previous suggestion in his test site.

    Cheers
    Ash

  • Wheel of Commerce

    Hi, and thanks for replying.

    I get that that line is to be inserted in wp-config.php only when I'm going to reorder the menu items. I've done it, and I still don't see the custom ones I've inserted when I'm logged in as super-admin -hence, I can't reorder what I can't even see. That's all that needs fixing right now: I need to be able to see the custom menu items I've inserted, at least whenever I need to reorder the. Something that I already mentioned, but that might be important to remember, is that even though I was seeing the custom menu items as super-admin before, the reorder function wasn't working, as I'd try to drag and drop the items and it simply didn't function -I was seeing them shaking when pressing the "Reorder menu items" button though.

    Thanks.

  • Ash

    Hello @Gabriel

    Would you please send me super admin login, a normal user login and ftp login so that I can try in your site?

    To send me details, please use our contact form: https://premium.wpmudev.org/contact/

    Select: I have a different question
    Subject: Attn-Ash (this ensures that it will be assigned to me)
    Details:
    1. Send all requested details
    2. Send a link of this thread so that I can track
    3. Send any other relevant link

    I will be happy to take a look :slight_smile:

    Cheers
    Ash

  • Wheel of Commerce

    Hi, and thanks for replying!

    It didn't work: after inserting that line in wp-config, the items still can't be moved and now I'm not even seeing the wp menu items as super-admin anymore. I also noticed that the WPMU Private Messaging was moved: it's now being displayed at the left of the site's title, not at the left of the chat icon anymore; did you manage to somehow move it?

    Thanks again.

  • Ash

    Hi there

    The above attached file worked perfectly fine in my test site, so I am getting confused now :slight_frown:

    Then I have tried to login again via FTP, I was able to change the UB file with original plugin file but it didn't change anything. As I am not sure if it is really updated successfully. I am seeing the attached error message frequently, this time it was Filezilla.

    Would you please send me your cpanel details in the same way you sent me admin info? Then I can try editing directly in the cpanel.

    Also, you didn't make changes in any other file, right?

    Cheers
    Ash

  • Wheel of Commerce

    Hi, man! Thanks for replying.

    It didn't work. I'm able to move the items, but then I click on Save and everything gets back to the way it was. By the way, I'm inserting the line below in my wp-config.php.

    define( 'ENABLE_NETWORK_ADMIN_VIEW', true );

    I'm inserting it in the beginning, as you can see below:

    <?php
    /** Enable W3 Total Cache */
    define( 'WPCACHEHOME', '/home/wheelofcommerce/wheelofcommerce.com/wp-content/plugins/wp-super-cache/' ); //Added by WP-Cache Manager
    define('WP_CACHE', true); // Added by W3 Total Cache
    
    /**
     * The base configurations of the WordPress.
     *
     * This file has the following configurations: MySQL settings, Table Prefix,
     * Secret Keys, WordPress Language, and ABSPATH. You can find more information
     * by visiting {@link http://codex.wordpress.org/Editing_wp-config.php Editing
     * wp-config.php} Codex page. You can get the MySQL settings from your web host.
     *
     * This file is used by the wp-config.php creation script during the
     * installation. You don't have to use the web site, you can just copy this file
     * to "wp-config.php" and fill in the values.
     *
     * @package WordPress
     */
    
     define( 'ENABLE_NETWORK_ADMIN_VIEW', true );
    
    // ** MySQL settings - You can get this info from your web host ** //
    /** The name of the database for WordPress */
    define('DB_NAME', 'wheelofcommerce');

    Thanks.

  • Ash

    Hello @Gabriel

    This is really strange! That was working in my test site, and even in your site when I added. Not sure if the cache plugin interfering with this functionality, though I don't think that would be any reason.

    I am tagging an available developer from second level support line in this thread for his valuable opinion on this issue. Please note that, developer response might be slower than usual staff response, so we appreciate your patience on this.

    Cheers
    Ash

  • Wheel of Commerce

    Update with what was discussed with Tyler during chat:
    It's all solved now, the issue was caused by the fact that I was pressing the Save button instead of that green V at the admin bar to save the changes.

    I'm writting the following for my own future reference, so I know all the changes I have to apply when the plugin is updated.

    Thanks. :slight_smile:

    "Tutorial on the modifications to be applied to Ultimate Branding > Custom Admin Bar evey time the plugin is updated
    1-Through FTP, go to /home/wheelofcommerce/public_html/wp-content/plugins/ultimate-branding/ultimate-branding-files/modules/custom-admin-bar-files/inc and replace the original UB_Admin_Bar.php with the edited UB_Admin_Bar.php. There's a copy of the edited file inside the same folder this tutorial is in (in my computer), and also in the post below:
    https://premium.wpmudev.org/forums/topic/make-custom-admin-bar-settings-be-applied-only-for-regular-users-not-for-admins?replies=48#post-865298

    2- When it's necessary to see the menu items I've inserted, to be able to reorder them, in wp-config.php, between
    * @package WordPress
    */

    and

    // ** MySQL settings - You can get this info from your web host ** //
    /** The name of the database for WordPress */
    [Sensitive information is removed by moderator]

    add the following:
    define( 'ENABLE_NETWORK_ADMIN_VIEW', true );

    Reorder the menu items as desired, then remove that line at wp-config.php –it's intended to be there only while the modifications are being made."

  • Wheel of Commerce

    Hi, man!

    I saw the latest update of Custom Admin Bar has the filter, but for some reason it isn't working for me. Can you access my site to see what's happening? The wordpress icon is being displayed at the bar too, and it wasn't before.
    Support access is active. Worst case scenario, I'll have to upload that edited file again and use the define line in the wp-config.

    Thanks. :slight_smile:

  • Sam

    hello @Gabriel

    Actually the plugin is working fine, but since u've installed BBPress you should note that "Key Master" role has administrative privileges and basically is "administrator" , that's why when u check it it's like checking "Administrator", what i did was unchecking "Key master" role to allow the menus to show up for the super admin.

    In the revision i'm releasing today i'm adding more clarifications to the roles listed in the adminbar module to prevent such issues.

    Cheers,
    Sam

  • Wheel of Commerce

    Oh, yes, it was being displayed before, now it isn't. It also wasn't displaying the Wordpress logo before and now it is, though only for logged out users.

    The idea is to have the admin bar as a tool bar for my site, that also displays a navigational menu showing the main pages, like the home page, forums, groups, different areas for the products (it's an ecommerce site) and so on. So the main pages will be displayed for logged out and logged in users, while stuff like the WPMU Private Messaging icon and the WPMU Wordpress Chat icon are obviously displayed only for logged in users.

    As you can see by the latest posts on this thread, Ashok modified one of the plugin's files to suit my needs, and it was working fine, but as Custom Admin Bar received an update that allows for a role filter (admin, subscriber, etc.), I was hoping I wouldn't need the customization anymore. Anyway, worst case scenario, if it can't be solved in any other way, I'll just use the previous method again -though if you guys have a better idea, that'd be great.

    Thanks.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.