Customize Wordpress Admin Bar Manually

You might have upgraded to the latest WordPress 3.4.2.This latest version does show an admin bar above when logged in to your wordpress dashboard as admin or author for your blog. There are some quick links listed to get quick access to your wordpress dashboard options without moving within dashboard. This is very handy admin bar to rapidly hover around your main site and admin area. This admin bar can also be customized which cripples its worth.

Here I will provide your some hacks to customize your wordpress admin bar for your subscribers or authors to be shown exactly you want. WordPress has not provided any options to customize this bar, that is, you cannot add or remove any links. But there are still ways to do so by creating functions. Here I will show, how you can customize the Admin bar the way you want.

Remove Existing Admin Bar Links

You can easily remove any and all links that are found by default on the Admin bar. In this example, I have removed the WordPress logo and Updates link. Go to functions.php file in wordpress theme directory and add this code anywhere.


function remove_admin_bar_links() {
 global $wp_admin_bar;
 $wp_admin_bar->remove_menu('wp-logo');
 $wp_admin_bar->remove_menu('updates');
}
add_action( 'wp_before_admin_bar_render', 'remove_admin_bar_links' ); 
 
IDs for other top links:
  • wp-logo: WordPress logo
  • my-account: Links to your account. The ID depends upon if you have avatar enabled or not.
  • site-name: Site name with other dashboard items
  • my-sites : My Sites menu, if you have more than one site
  • get-shortlink : Shortlink to a page/post
  • edit : Post/Page/Category/Tag edit link
  • new-content : Add New menu
  • comments : Comments link
  • updates : Updates link
  • search: Search box
  • user-info: Links to username and login id with Avatar in my account menu

Hide Admin Bar for non-admins

As we may need to do not redirect subscribers or authors to dashboard area we can then remove all the links  and replace with our own links and redirection. We can simply hide wordpress default admin links and replace them with our own front end links to our custom front end login page templates for authors or subscribers. In the same functions.php file we can update above code like this:

if (!is_admin() && is_user_logged_in()) {
    add_filter('show_admin_bar', '__return_true');
    function admin_bar_render() {
    global $wp_admin_bar;
    // Remove Individual Admin bar items
    $wp_admin_bar->remove_menu('comments');
    $wp_admin_bar->remove_menu('dashboard');
    $wp_admin_bar->remove_menu('site-name');
    $wp_admin_bar->remove_menu('new-content'); // Completely Removes '+ New'
    $wp_admin_bar->remove_menu('new-post');
    $wp_admin_bar->remove_menu('new-media');
    $wp_admin_bar->remove_menu('wp-logo');
    $wp_admin_bar->remove_node('user-info');
// My Account Menu Item Redirects to Frontend Dashboard
    $my_account_menu = $wp_admin_bar->get_node('my-account');
    $account_href = get_option('siteurl') . '/members-dashboard/';
    $my_account_menu->href = $account_href;
    $wp_admin_bar->add_menu($my_account_menu);
    // Eidt Profile Menu Item Redirects to Frontend Profile Edit
    $edit_profile_menu = $wp_admin_bar->get_node('edit-profile');
    $profile_href = get_option('siteurl') . '/profile/';
    $edit_profile_menu->href = $profile_href;
    $wp_admin_bar->add_menu($edit_profile_menu);
    // Logout Redirect to Frontend Login Page
    $logout_menu = $wp_admin_bar->get_node('logout');
    $logout_href = wp_logout_url( home_url('/login') );
    $logout_menu->href = $logout_href;
    $wp_admin_bar->add_menu($logout_menu);
    }
    add_action( 'wp_before_admin_bar_render', 'admin_bar_render' );
This will then be shown like this:

wordpress admin bar
User Front End Area
 

Add Single Links on the Admin Bar


To add a new custom link on the WordPress admin bar, drop the following function in your theme’s functions.php file.

function add_sumtips_admin_bar_link() { 
global $wp_admin_bar; 
if ( !is_super_admin() || !is_admin_bar_showing() ) 
return; 
$wp_admin_bar->add_menu( array( 'id' => 'sumtips_link', 
'title' => __( 'SumTips Menu'), 
'href' => __('http://sumtips.com'), ) ); 
add_action('admin_bar_menu', 'add_sumtips_admin_bar_link',25);

Change the id, title and href value with your own.
أحدث أقدم