Most WordPress plugin tutorials tell you to add your option pages to the Settings submenu, and that’s that. Did you know that add_options_page()
isn’t your only choice?
Here are the functions to add pages to all the major submenus:
<br /> <?php<br /> //Settings<br /> add_options_page (page_title, menu_title, capability, handle);<br /> // Tools<br /> add_management_page (page_title, menu_title, capability, handle);<br /> // Appearance<br /> add_theme_page (page_title, menu_title, capability, handle);<br /> // Posts<br /> add_posts_page (page_title, menu_title, capability, handle);<br /> // Pages<br /> add_pages_page (page_title, menu_title, capability, handle);<br /> // Users<br /> add_users_page (page_title, menu_title, capability, handle);<br /> ?><br />
The page_title is the text that will appear as the <title>
and <h2>
tags on your option page. The menu_title is the text that will appear as an item in the navigation menu — you might use a shorter phrase there. The capability is a relevant capability a user should have in order to do stuff with your plugin options. (It’s not the role, because lots of plugins modify the basic roles, so it’s safer to check for a specific capability.)
For example, if you’re creating a plugin that manages user roles and capabilities, you might call your option page Role Manager, and the function containing your the form markup might be ‘role_manager_plugin_options’. If you wanted to put the page under Users, you’d write the following:
<br /> // add the page to the navigation menu<br /> add_action('admin_menu', 'role_manager_plugin_add_page');</p> <p>function role_manager_plugin_add_page() {<br /> // Add a new submenu under Users:<br /> add_users_page('Role Manager Options', 'Role Manager', 'edit_users', 'role_manager_plugin_options');<br /> }<br />
(Updated to correct add_user_page() to add_users_page(). Thanks, Ozh!)
This is part of the chapter on creating plugins in Beginning WordPress 3.
Ozh says
Hu… I hope this isn’t a copy/paste of what you actually put in your book because this is all wrong :)
First, it’s not add_user_page() but add_users_page()
Second, a parameter is missing, you need to define a slug for the page then a function that will output the page. Omitting the function parameter will cause WP to assume just including the file without calling any function will be enough to draw the menu, but this is just whacky to do it this way.
steph says
I’m pretty sure it’s in the book, but I’ll have to go check. I’m also certain that I got the functions straight out of the Codex.