WordPress Development: Creating Top-Level Menu Items
Posted on March 25th, 2009
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
- Web Development: Cross Browser Testing - January 13th, 2010
- Moving Beyond SSH: Secure Delete - January 12th, 2010
- WordPress Development: Managing Plugins through Subversion - January 5th, 2010
- Everything PHP: Multiple Table Joins - December 30th, 2009
- Web Development: Programming Software - December 15th, 2009
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
3 Comments
Sorry, the comment form is closed at this time.
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!