Make toolbar items visible based on user role?

My site has different roles where some people pay to have extra features available.
One tool I would like to add is for creating landing pages and the plugin adds a link in the left admin menu which I know how to hide but there is also a link/icon on the toolbar with a drop down menu when creating/adding pages.

Each of the toolbar buttons have their own ID such as:
<button id="mceu_16-open" tabindex="-1" type="button" role="presentation">

So you guys probably have a cool function to hide that based on the users role? I just have 2 roles setup and want to make it available to one & not the other.

thanks!!!

  • Sajid

    Hi George! Hope you are doing good today and thanks for your question :slight_smile:

    Yes you can remove some menu items based on user role. To do this I need the node ID (unique menu ID of item). You can find the node ID in source of HTML. For example if we want to hide wp logo then ID will be "wp-logo" removing the "wp-admin-bar-" from start of list item ID.

    <li id="wp-admin-bar-wp-logo" class="menupop"> … </li>

    If you are unable to find the node ID then grant support access of your website and provide screenshot of menu item that you want to hide or show. You can grant support access by following the steps mentioned in this manual.
    https://premium.wpmudev.org/manuals/wpmu-dev-dashboard-enabling-staff-login/

    Take care and have a great weekend :slight_smile:

    Cheers, Sajid

  • George

    Thanks Sajid!
    This is the source code for the button I want to hide and make visible for user with a role="pro".

    Is this the info you need?

    <div id="mceu_17" class="mce-widget mce-btn mce-menubtn mce-last" aria-labelledby="mceu_17" tabindex="-1" role="button" aria-label="WPSalesToolBox" aria-haspopup="true">
    <button id="mceu_17-open" tabindex="-1" type="button" role="presentation">
    <i class="mce-ico mce-i-icon wsalestoolbox-own-icon"></i>
    <span></span>
    <i class="mce-caret"></i>
    </button>
    </div>

    I did grant support access if needed. This is a copy of my live site so I can test things out here first.
    http://evantageaz.com/

    thanks!

  • Sajid

    Hi George! Hope you are doing good today and welcome back :slight_smile:

    I am sorry for confusion here. I thought you want to remove some items from WordPress adminbar/toolbar. So you want to remove items from post/page editor ?

    Any way add below code functions.php file of your WordPress theme or child theme (recommended). This code will hide that button for all other users except 'administrator' and 'pro'.

    if( !function_exists('wpmu_hide_itmes') ):
    	function wpmu_hide_itmes($buttons)
    	 {	
    
    		  //do not remoe any button if its admin or pro users
    		  if(current_user_can('pro') or current_user_can('administrator')) return $buttons;
    
    		  //Remove the text color selector
    		  $remove = array( 'icon' );
    
    		  //Find the array key and then unset
    		  if($remove){
    			  foreach ($remove as $vlaue){
    
    				   if ( ( $key = array_search($vlaue,$buttons) ) !== false )
    						unset($buttons[$key]);
    			  }
    		  }
    
    		  return $buttons;
    	 }
    endif;
    add_filter('mce_buttons','wpmu_hide_itmes');

    You can hide more buttons by adding button "key" in $remove array preceding "," before each key element. You can find button key in source of HTML of editor. In above code mce-i-icon is key after removing "mce-i-".

    Take care and have a nice day :slight_smile:

    Cheers, Sajid

  • George

    Sorry, I should have included a screenshot to avoid confusion.
    However, all users are administrators by default when they create a site so I would NOT want to make this button available to them, only to PRO users or maybe "Super Admin" instead of "Adminstrator".

    So when a site is created they are 'administrator' by default, then they have the option to upgrade to add the additional role of "pro". So only "pro" & "superadmin" would have the button visible and it would be hidden from 'administrator'.

    Can we change that?

    Also, am I suppose to edit this and add the key, is this the key 'wsalestoolbox-own-icon' from the above code?

    if($remove){
    			  foreach ($remove as $vlaue){
    
    				   if ( ( $key = array_search($vlaue ,$buttons) ) !== false )
    						unset($buttons[$key]);
    			  }
    		  }

    Is this what it should be:
    if ( ( $key = array_search($vlaue wsalestoolbox-own-icon,$buttons) ) !== false )
    Thanks!

  • Sajid

    Hi George! Hope you had a great weekend :slight_smile:

    So when a site is created they are 'administrator' by default, then they have the option to upgrade to add the additional role of "pro". So only "pro" & "superadmin" would have the button visible and it would be hidden from 'administrator'.

    Can we change that?

    Yes! Use below code instead for super admin and pro users.

    if( !function_exists('wpmu_hide_itmes') ):
    	function wpmu_hide_itmes($buttons)
    	 {	
    
    		  //do not remoe any button if its super admin or pro users
    		  if(current_user_can('pro') or is_super_admin() ) return $buttons;
    
    		  //Remove the text color selector
    		  $remove = array( 'icon' );
    
    		  //Find the array key and then unset
    		  if($remove){
    			  foreach ($remove as $vlaue){
    
    				   if ( ( $key = array_search($vlaue,$buttons) ) !== false )
    						unset($buttons[$key]);
    			  }
    		  }
    
    		  return $buttons;
    	 }
    endif;
    add_filter('mce_buttons','wpmu_hide_itmes');

    Also, am I suppose to edit this and add the key, is this the key 'wsalestoolbox-own-icon' from the above code?

    This code will work out of the box for you to remove this id="mceu_17" item. You do not need to do any thing for now :slight_smile:

    But in future, if you want to remove any other item from toolbar, then you can do it like this.

    1. Find the key of mce item. For example in source code posted by you "mce-i-icon" (notice the bold item) is key. So find this key for any other item that you wish to remove, find its key.
    2. Now we have key of item. Add this key in code posted by me in this post, in below line.
    $remove = array( 'icon' );
    Your code will look like this after adding another key(s).
    $remove = array( 'icon' , 'key2', 'key3', ...);

    Hope its clear or feel free to post back here if you want to remove any other item from toolbar in future :slight_smile:

    Cheers, Sajid

  • George

    I'm sorry, this doesn't seem to be working and when I checked my source code again the item# has changed from 17 to 16. I just updated a few plugins but I guess something changed order of my buttons?

    This is for the visual editor toolbar, the tiny mce buttons.
    Here is the code now and I will attach a screen shot:

    <button id="mceu_16-open" tabindex="-1" type="button" role="presentation">
    <i class="mce-ico mce-i-icon wsalestoolbox-own-icon"></i>
    <span></span>
    <i class="mce-caret"></i>
    </button>
    </div>

    Can you help me one more time :slight_smile: thanks!!

  • George

    Sorry, too late to update my prev post so have to post again.
    Ok, I get how this works now and the function does work if I am removing the standard buttons like "bold" or "link" and use the keys:
    $remove = array( 'bold','link' );
    that will work.
    Now if I try to use the key for any of my buttons that were added by plugins I installed, they do not work.
    So this is what I have but 'icon' does not work like the others?
    <i class="mce-ico mce-i-icon wsalestoolbox-own-icon"></i>
    Figured I would try 'icon wsalestoolbox-own-icon' & 'wsalestoolbox-own-icon'
    but that didn't work either.

    Tried a another button added by a slider:
    <i class="mce-ico mce-i-msp_shortcodes_button"></i>
    Key would be 'msp_shortcodes_button' right?
    That didn't work either but all the default buttons are able to be removed.
    Thanks!!

  • Sajid

    Hi George! Hope you are doing good today :slight_smile:

    The order of button does not matter, what matter is the KEY and our code should run after code of your plugin initialized & icon has been added. I think this is the case, specially when default buttons are being removed by our code.

    Replace below line of the code posted above:
    add_filter('mce_buttons','wpmu_hide_itmes');

    With:
    add_filter('mce_buttons','wpmu_hide_itmes', 100);

    <i class="mce-ico mce-i-msp_shortcodes_button"></i>
    Key would be 'msp_shortcodes_button' right?

    Yes! this is the KEY :slight_smile:

    Please try the above changes first. If its not working send me FTP and Login (WordPress admin username/password) via our private contact form, in following format.

    Subject: "Attn: Sajid Javed"
    -WordPress admin username
    -WordPress admin password
    -login url
    -FTP credentials (host/username/password)
    -link back to this thread for reference
    -any other relevant urls

    Select "I have a different question" for your topic - this and the subject line ensure that it gets assigned to me :simple_smile:

    https://premium.wpmudev.org/contact/

    Take care and have a great weekend :slight_smile:

    Cheers, Sajid

  • George

    Would you be able to help me hide another button?
    This is the html from the button on the toolbar:

    <button tabindex="-1" type="button" role="presentation">
    <i class="mce-ico mce-i-none" style="background-image: url('http://evantageaz.com/default-template/wp-content/plugins/zedity/mce/zedity-logo.png?4.1')"></i>
    </button>

    and this is the function that you created for me previously in my functions.php:

    if( !function_exists('wpmu_hide_itmes') ):
    	function wpmu_hide_itmes($buttons)
    	 {	
    
    		  //do not remoe any button if its super admin or pro users
    		  if(current_user_can('pro') or is_super_admin() ) return $buttons;
    
    		  //Remove the text color selector
    		  $remove = array( 'msp_shortcodes_button', 'wsalestoolbox_tc_button', 'none' );
    
    		  //Find the array key and then unset
    		  if($remove){
    			  foreach ($remove as $vlaue){
    
    				   if ( ( $key = array_search($vlaue,$buttons) ) !== false )
    						unset($buttons[$key]);
    			  }
    		  }
    
    		  return $buttons;
    	 }
    endif;
    add_filter('mce_buttons', 'wpmu_hide_itmes', 100);

    Can you tell from that info what needs to be added the functions.php ?
    I granted support access in case you need it again, its just a test site: evantageaz.com

    Thanks!!!

  • Sajid

    Hi @George!

    Hope you are doing good and welcome back :slight_smile:

    $remove = array( 'msp_shortcodes_button', 'wsalestoolbox_tc_button', 'none' );

    In above code of your function replace "none" with "ico".

    Please bear in mind, as stated below the key can not be accurate in HTML. I have print all keys of by using below code to get the exact key.

    echo '<pre>';
    print_r($buttons);
    exit;

    Note: Above code will display the list of buttons keys and terminate the execution of rest your website. In general it will break your website.

    So be aware if you use this code and remove it, to get the site back to normal :slight_smile:

    If you are not sure how to do this then please send me FTP details again and also let me know what button you want to remove exactly by marking a screenshot ?

    Take care and have a nice day :slight_smile:

    Cheers, Sajid

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.