WordPress Development: Creating Top-Level Menu Items

Published on Mar 25, 2009   //  Development, WordPress

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:

[code=’php’]add_menu_page(page_title, menu_title, capability, file, [function], [icon_url]);[/code]

  • 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 :

[code=’php’]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’));
}[/code]

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).

3 Comments to “WordPress Development: Creating Top-Level Menu Items”

  • 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

  • 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.

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