Customize WordPress Admin Bar by Adding/Removing Links

Print View Mobile View

You might have already updated to the latest WordPress 3.1. This version comes with a handy admin bar that provides signed-in users quick access to some Admin features, directly from the main site.

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.

Add Single Links on the Admin Bar

Custom Link on Admin Bar
Custom Link on Admin Bar

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

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.

Create Link Submenus on the Admin Bar

Submenu Links

Now here, we will first create two sub-items (“View All Posts” and “Downloads”) for the link “SumTips Menu.” Then again, add another new link (“Browsers”) as a child item to the “Downloads” menu.  This will create a two-level menu on the Admin bar.

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 sub menu link "View All Posts"
	$wp_admin_bar->add_menu( array(
		'parent' => 'sumtips_link',
		'id'     => 'sumtips_all',
		'title' => __( 'View All Posts'),
		'href' => __('http://sumtips.com/all'),
	));

	// Add sub menu link "Downloads"
	$wp_admin_bar->add_menu( array(
		'parent' => 'sumtips_link',
		'id'     => 'sumtips_downloads',
		'title' => __( 'Downloads'),
		'href' => __('http://sumtips.com/downloads'),
		'meta'   => array(
			'class' => 'st_menu_download',),
	));
		$wp_admin_bar->add_menu( array(
			'parent' => 'sumtips_downloads',
			'id'     => 'sumtips_browsers',
			'title' => __( 'Browsers'),
			'href' => __('http://sumtips.com/downloads?category=3'),
		));
}
add_action('admin_bar_menu', 'add_sumtips_admin_bar_link',25);

You can add more links and submenus by extending this single function as per your needs.

We can also pass parameters to the link items, useful for applying CSS. An example of this can be seen above for “Downloads” link.

Remove Links from the Admin Bar

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.

Remove link from WordPress Admin Bar
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

Get Delete Link on ‘Posts’ and ‘Page’ Pages

Delete link on WordPress Admin Bar

With the following code snippet, you can add a quick-delete button on all posts and pages. Button stays hidden on all other pages of your site.

function admin_bar_delete_option() {
  global $wp_admin_bar;
  if ( !is_super_admin() || !is_admin_bar_showing() )
      return;
  $current_object = get_queried_object();
  if ( !empty( $current_object->post_type ) &&
     ( $post_type_object = get_post_type_object( $current_object->post_type ) ) &&
     current_user_can( $post_type_object->cap->edit_post, $current_object->ID )
  ) {
    $wp_admin_bar->add_menu(
        array( 'id' => 'delete',
            'title' => __('Delete'),
            'href' => get_delete_post_link($current_object->term_id)
        )
    );
  }
}
add_action( 'admin_bar_menu', 'admin_bar_delete_option', 40 );

Move Admin Bar to the Bottom of Page

Admin Bar at the Bottom of Page
function move_admin_bar() {
    echo '<style type="text/css">
	body {
		margin-top: -28px;
		padding-bottom: 28px;
	}
	body.admin-bar #wphead {
		padding-top: 0;
	}
	body.admin-bar #footer {
		padding-bottom: 28px;
	}
	#wpadminbar {
		top: auto !important;
		bottom: 0;
	}
	#wpadminbar .quicklinks .menupop ul {
		bottom: 28px;
	}
	</style>';
}
add_action( 'admin_head', 'move_admin_bar' );
add_action( 'wp_head', 'move_admin_bar' );

That’s all. 🙂

122 thoughts on “Customize WordPress Admin Bar by Adding/Removing Links”

Comments are closed.