WordPress Development: Creating Top-Level Menu Items

Posted on March 25th, 2009

WordPress Development

Previously we talked about adding an options page, but today we’ll be talking about how to create top-level menu items.

At some point you may need to create a complex plugin with quite a few options. You may have too many options to fit on just one page under Settings. If you come across this situation, you should consider create a dedicated top-level menu item for your plugin. This way you can organize your many options in to separate pages all under one group. This will cause less confusion for the end-user.

Adding a top-level menu is fairly simple, and has some of the same principles of adding an options page. The function we use to add a top-level menu is add_menu_page(). The syntax is as follows:

add_menu_page(page_title, menu_title, capability, file, [function], [icon_url]);
  • page_title – The title that will appear on the options page
  • menu_title – The title of the menu item
  • capability – The capability required to view this options page (usually manage_options is used)
  • file – The file the function can be found in. Use __FILE__ for the current file
  • [function] – Optional. The function to create the options page
  • [icon_url] – Optional. In WordPress 2.7 an icon is shown beside top-level menu items. You can define a custom one here (use the full URL to it, try to keep the icon in the plugin’s folder)

Remember that we still have to wrap this function inside of a function and hook into admin_menu. Let’s look at an example (using a fictional Polls plugin as :

add_action('admin_menu', 'mgf_add_menu');
function mgf_add_menu() {
add_menu_page('Polls Options', 'Polls', 'manage_options', __FILE__, '', plugins_url('polls/menu-icon.png'));
}

Then there you have it, your own menu item. If you’re wondering, plugins_url() will give you the URL to the plugins directory appended by any path you give it.

Next week we’ll be going over how to add submenus to this custom top-level menu items, and also to the default top-level menu items (other than Settings).

Last 5 posts by Matt Freedman

Posted in Development, WordPress | 1,899 views | | Digg This | del.icio.us | Technorati


Related Topics:
WordPress Development: Submenu Items
WordPress Development: Creating Options Pages
WordPress Development: IRC Development Chats
WordPress Development: Plugin Theme Functions
WordPress Development: Subversion Patches

RSS feed

3 Comments

Gravatar
Comment by bj maslin ~ come follow me on twitter @bjm16
2009-03-26 10:23:51

Thanks for the info about wordpress although I have to admit I’ll have to read over it
a few times before I can apply it.
I’m not the most astute techie in the world, an incredible understatement. I will be
checking your site for more tips.
Thanks.
bjm16

 
Gravatar
Comment by seo joel Subscribed to comments via email
2009-03-27 08:43:24

Great, I am creating a solar website right now that needs main and sub menus in Wordpress, and I am not a programmer by any means. This will be great information to see.

 
Gravatar
Comment by Minnesota Attorney
2009-03-28 05:51:08

This blog is one of the most useful that I have seen for Wordpress Plugin developers and innovative Wordpress development ideas. Great instructions!

 

Sorry, the comment form is closed at this time.

© 2007 BlueFur Hosting | Privacy Policy
Theme by Unique Blog Designs