Admin Bar tutorial part 1

going to slowly start building the tutorial here.

So off we go with part 1

  • Tom Eagles

    There have been a lot of posts recently about the admin bar and customising it. There are many code snippets scattered around the internet on how to modify it

    unfortunately they all require editing the functions.php of the theme in question, which means that every time you update your themes then you have to copy n paste that code back into each theme.

    For example this one hides the admin bar if you are not an editor on an admin on a site.

    // show admin bar only for admins
    if (!current_user_can('manage_options')) {
    	add_filter('show_admin_bar', '__return_false');
    }
    // show admin bar only for admins and editors
    if (!current_user_can('edit_posts')) {
    	add_filter('show_admin_bar', '__return_false');
    }

    But we dont want that !! so next we are going to create a plugin that will handle this for us, where we can set who we want or dont want to see the admin bar. And combine that with some custom menu items.

    But as i said this is just the beginning part to get warmed up.

    Cheers

    Tom

    Edited the original part made a mistake on a call to the function which left a small potential exploit.

  • Tom Eagles

    Ok so whats the ideal situation, well a plugin that we could install that will either act globally or on a per site basis.

    So here is the code for the plugin, I've labelled it so you can follow it through hopefully.

    There are many examples out there that will do the same thing but this will hopefully give you an idea so when we move onto the next parts you will have some idea of the workflow.

    <?php
    /* Customise the admin tool bar by user role for new and existing users Tutorial at WPMUDEV */
    /*
    Plugin Name: Hide the  Admin tool bar for different users based on role
    Plugin URI: http://webfx.cz/
    Description: Simplified method to remove the tool bar based on user role, without the need to edit the functions.php of a theme</a>.
    Author: Tom Eagles
    Version: Tutorial
    Author URI: http://wpmudev.org */ 
    
    /* Notes: the benefit of this method is either we can network activate the plugin or activate it on a per site basis no need for editing the themes functions.php anymore */
    
    /* Ok lets set this up under the tools menu of course you can modify this yourself to place it anywhere */
    
    add_action('admin_menu', 'customise_admin_tool_bar_submenu');
    function customise_admin_tool_bar_submenu() {
    	$parent_slug="tools.php";
    	$page_title="Cutomise the Admin Tool Bar";
    	$menu_title="Customise the Admin tool bar";
    	$capability="activate_plugins";
    	$menu_slug="Customise_the_Admin_tool_Bar";
    	$function="customise_the_admin_tool_bar";
    	add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function );
    }
    
    /* Okay lets setup some basics */
    
    // This is where we get the user's role string based on the user's ID 
    
    function customise_the_admin_tool_bar_get_user_role($user_ID) {
    	global $wpdb;
    	$metaname = $wpdb->prefix."capabilities";
    	$capabilities = get_user_meta($user_ID, $metaname, true);
    	if ($capabilities['administrator']) return "administrator";
    	if ($capabilities['editor']) return "editor";
    	if ($capabilities['author']) return "author";
    	if ($capabilities['contributor']) return "contributor";
    	if ($capabilities['subscriber']) return "subscriber";
    	return false;
    }
    
    // This is where we will handle the default settings for new users by default based on their roles.
    
    add_action('user_register','customise_the_admin_tool_bar_default_register');
    function customise_the_admin_tool_bar_default_register($user_ID) {
    	$customise_the_admin_tool_bar_default_roles = get_option('customise_the_admin_tool_bar_default_roles');
    	if (!customise_the_admin_tool_bar_default_roles) $customise_the_admin_tool_bar_default_roles = array();
    	$role = customise_the_admin_tool_bar_get_user_role($user_ID);
    	if (in_array($role, $customise_the_admin_tool_bar_default_roles)) update_user_meta( $user_ID, 'show_admin_bar_front', 'false' );
    }
    
    /*Show the settings page update the settings when the plugin user clicks save */
    
    function customise_the_admin_tool_bar() {
    	$customise_the_admin_tool_bar_default_roles = get_option('customise_the_admin_tool_bar_default_roles');
    	if (!$customise_the_admin_tool_bar_default_roles) $customise_the_admin_tool_bar_default_roles = array();
    	?>
    
    <div id="customise_the_admin_tool_bar" class="wrap"> 
    
      <!-- Add some basic styling to the page we will just add a margin to line things up -->
    
      <style type="text/css">
    	#customise_the_admin_tool_bar, #customise_the_admin_tool_bar fieldset { margin-left: 25px; }
    	#customise_the_admin_tool_bar #tools { margin-left: -25px; }
    	#customise_the_admin_tool_bar #response { margin-right: 45px; }
    	#customise_the_admin_tool_bar .whattodo { font-size: 110%; }
    	</style>
    
      <!-- Add some basic styling to the page -->
    
      <div id="tools" class="icon32"><br />
      </div>
      <h2>Customise the admin tool bar based on user role</h2>
      <p class='whattodo'>select if you wish the top toolbar to be displayed by setting your site users' preferences.</p>
      <?php
     /* ok here we need to handle all the users that exist in the WordPress Database */
    	if (!empty($_POST['customise_the_admin_tool_bar_turn_off']) && current_user_can('edit_users')) {
    		$users_affected = "";
    		foreach ($_POST['customise_the_admin_tool_bar_turn_off'] as $role) {
    			$users = get_users(array("role"=>"$role"));
    			foreach ($users as $u) {
    				update_user_meta( $u->ID, 'show_admin_bar_front', 'false' );
    				$users_affected .= $u->user_login.", ";
    			}
    		}
    		$users_affected = substr($users_affected,0,-2);
    		echo "<div id='response' class='updated'><p><strong>Settings for your existing users have been saved.</strong> The user(s) that you modified are: $users_affected</p></div>";
    	}
    	if (!empty($_POST['customise_the_admin_tool_bar_off_by_default']) && current_user_can('edit_users')) {
    		$customise_the_admin_tool_bar_default_roles = $_POST['customise_the_admin_tool_bar_turn_off_by_default'];
    		update_option( "customise_the_admin_tool_bar_default_roles", $customise_the_admin_tool_bar_default_roles);
    		echo "<div id='response' class='updated'><p><strong>The new default user settings have been saved.</strong></p></div>";
    	}
    	?>
    
      <!-- Simple form to handle the processing -->
      <!-- Remember because we are using check boxes we need to setup an array to find out which was checked and so we have to add a [] after each checkbox so we can grab its id to use -->
    
      <form name='customise_the_admin_tool_bar_settings' id='customise_the_admin_tool_bar_settings' method='post' action=''>
        <p>Hide the admin bar for <strong>existing users</strong> with the following user roles: </p>
        <fieldset>
          <p>
            <label>
              <input type='checkbox' name='customise_the_admin_tool_bar_turn_off[]' value='administrator' />
              Administrator</label>
          </p>
          <p>
            <label>
              <input type='checkbox' name='customise_the_admin_tool_bar_turn_off[]' value='editor' />
              Editor</label>
          </p>
          <p>
            <label>
              <input type='checkbox' name='customise_the_admin_tool_bar_turn_off[]' value='author' />
              Author</label>
          </p>
          <p>
            <label>
              <input type='checkbox' name='customise_the_admin_tool_bar_turn_off[]' value='contributor' />
              Contributor</label>
          </p>
          <p>
            <label>
              <input type='checkbox' name='customise_the_admin_tool_bar_turn_off[]' value='subscriber' />
              Subscriber</label>
          </p>
        </fieldset>
        <p>We will Hide the admin bar by default for <strong>any new users</strong> with the following roles: </p>
    
        <!--We will set all to checked here so the admin can simply uncheck the ones he doesnt want to hide the menu from -->
    
        <fieldset>
          <p>
            <label>
              <input type='checkbox' name='customise_the_admin_tool_bar_turn_off_by_default[]' value='administrator' <?php if (in_array('administrator',$customise_the_admin_tool_barl_default_roles)) echo ' checked="checked";' ?>/>
              Administrator</label>
          </p>
          <p>
            <label>
              <input type='checkbox' name='customise_the_admin_tool_bar_turn_off_by_default[]' value='editor' <?php if (in_array('editor',$customise_the_admin_tool_bar_default_roles)) echo ' checked="checked";' ?>/>
              Editor</label>
          </p>
          <p>
            <label>
              <input type='checkbox' name='customise_the_admin_tool_bar_turn_off_by_default[]' value='author' <?php if (in_array('author',$customise_the_admin_tool_bar_default_roles)) echo ' checked="checked";' ?>/>
              Author</label>
          </p>
          <p>
            <label>
              <input type='checkbox' name='customise_the_admin_tool_bar_turn_off_by_default[]' value='contributor' <?php if (in_array('contributor',$customise_the_admin_tool_bar_default_roles)) echo ' checked="checked";' ?>/>
              Contributor</label>
          </p>
          <p>
            <label>
              <input type='checkbox' name='customise_the_admin_tool_bar_turn_off_by_default[]' value='subscriber'<?php if (in_array('subscriber',$customise_the_admin_tool_bar_default_roles)) echo ' checked="checked";' ?> />
              Subscriber</label>
          </p>
        </fieldset>
        <br />
        <p>
          <input type="submit" id="submit" value="Save Changes" class="button-primary" />
        </p>
        <br />
      </form>
    </div>
    <?php
    }
    ?>

    ok so now what do we do with this open up a text editor and copy and paste this into it, once you have done that, create a new folder called "Hide the Admin tool bar for different users based on role" and save the file as "hide_the_admin-bar_based_on_user_role.php" in the folder, then zip the file.

    Then simply go to your dashboard>plugins>add new> and upload this zip.

    Now for the cool part you have some choices either activate it straight away on a single site or if in a multisite you can network activate it so everyone can use it, or do it on a site by site basis.

    So now we can hide it based on roles, dont have to worry about messing with functions.php anymore.

    So what about adding items to existing menus or creating new menu items etc.

    Well that as they say is another story or actually part 2.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.