Recently i added some action hooks to dropdown menu widget. Using those hooks you can easily add menu items to the menu and also subscribe buttons or search boxes. Here in this step by step tutorial i will show you how to add a simple administration menu to your dropdown widget.

This tutorial needs your site have dropdown menu widget installed and working. So if you didn’t install it yet you can get it from here.

To add an admin menu to the widget we will use “dropdown_list_after” action hook. This hook runs right after your menu items so you can add additional list items to the dropdown.

Open “functions.php” file in your theme directory and simply add this action hook:

[php light=”true” title=””]add_action(‘dropdown_list_after’, ‘my_admin_menu’);

This will tell wordpress to run my_admin_menu function to run when dropdown_list_after is called.

Now we will create our function called my_admin_menu:

function my_admin_menu(){
  // add admin menu items here
}

Before starting admin menu items, it would be better if we checked admin is logged in. Otherwise it will show up to all regular users. We can check it using current_user_can wordpress function:
[php light=”true” title=””] if( current_user_can( ‘create_users’ ) ){ // This should be admin. right?

Now that we know current user is admin, we start echoing our menu items:

[php light=”true” title=””] echo ‘<li><a href="/wp-admin/index.php">Admin</a><ul>’; // Start with dashboard on top 😉

So this will be our top item. Every unordered list (ul) will be a sub level. Here we started our sublevel items too. Let’s go on:

	echo '<li><a href="/wp-admin/post-new.php">Add New..</a><ul>';
		echo '<li><a href="/wp-admin/post-new.php">Post</a></li>';
		echo '<li><a href="/wp-admin/post-new.php?post_type=page">Page</a></li>';
		echo '<li><a href="/wp-admin/link-add.php">Link</a></li>';
	echo '</ul></li>';

Here we added a branch to add new posts, pages and links. All nested under “Add New.. ” top level. Let’s add some more admin items:

	echo '<li><a href="/wp-admin/themes.php">Themes</a></li>';
	echo '<li><a href="/wp-admin/widgets.php">Widgets</a></li>';
	echo '<li><a href="/wp-admin/plugins.php">Plugins</a></li>';

If you agree that’s enough we will close the sub levels. If you like you can continue adding lines to this. After closing sub level <ul> we should close containing li item too.

	echo '</ul></li>'; 

We have done adding items to the menu and now it is automagically dropdown!

Here is the full code for your ease:

function my_admin_menu(){
  if( current_user_can( 'create_users' ) ){ // This should be admin. right?
	// add admin menu items here
	echo '<li><a href="/wp-admin/index.php">Admin</a><ul>'; // Start with dashboard on top ;) and start sub items
	echo '<li><a href="/wp-admin/post-new.php">Add New..</a><ul>'; // Start another sub level here
		echo '<li><a href="/wp-admin/post-new.php">Post</a></li>';
		echo '<li><a href="/wp-admin/post-new.php?post_type=page">Page</a></li>';
		echo '<li><a href="/wp-admin/link-add.php">Link</a></li>';
	echo '</ul></li>'; // Close 2nd sub level
	echo '<li><a href="/wp-admin/themes.php">Themes</a></li>';
	echo '<li><a href="/wp-admin/widgets.php">Widgets</a></li>';
	echo '<li><a href="/wp-admin/plugins.php">Plugins</a></li>';
	echo '</ul></li>'; // Close the top level
 } // Closing if
}
add_action('dropdown_list_after', 'my_admin_menu'); 

You can also use other hooks registered with the plugin:

  • dropdown_before – Runs in the main container before the div tag containing main dropdown list.
  • dropdown_list_before – Runs when ul.dropdown is started. You should echo li items.
  • dropdown_list_after – Runst at the end of dropdown menu. I showed you how to use this hook here.
  • dropdown_after – Runs after dropdown container is closed.

You can find more information about action hooks on wordpress codex.