Friday, July 5, 2013

Options panel day 5 - wordpress menu types

On part 3 of the settings api tutorials and this part goes over menu types (not the settings api but very closely related).

Top-level menus: are the menus in the left column that hold sub-menu pages.  You use the add_menu_page function to add your own: http://codex.wordpress.org/Function_Reference/add_menu_page
add_menu_page(Page Title, Menu Title, Capability, Menu Slug, Callback, Icon URL, Position);
  • Page Title - Title displayed in the browser window tab.
  • Menu Title - Title displayed in the menu.  Keep it short so it doesn't wrap and look stupid.
  • Capability - String value that specifies which users can access the menu.  Roles: http://codex.wordpress.org/Function_Reference/add_menu_page
  • Menu Slug - identifier for the menu page.  Is a hook that submenu pages use to register themselves.
  • Callback - function that defines content for the page.  This can be html or refer to and external file.
  • Icon URL - path of the icon that gets displayed next to menu title.  (optional)
  • Position - place in the menu where the page gets placed.  It gets placed at the bottom by default but custom positioning allows you to put it above or below and existing item.
Use this function in a custom function you create that is hooked to the admin_menu.  A lot of people think that the position argument is a bad practice cause it changes the traditional wordpress experience and the position can be overwritten if defined somewhere else.

Submenus: pretty much the same as a menu except they belong to top-level menus (they always have a parent).  add_submenu_page is the function that is used for submenus: http://codex.wordpress.org/Function_Reference/add_submenu_page
add_submenu_page(Parent Slug, Page Title, Menu Title, Capability, Menu Slug, Callback);
  • Parent Slug - ID of the menu item it will belong to.
  • Page Title - same as top-level.
  • Menu Title - same as top-level.
  • Capability - refers to what types of users can access the specific submenu page
  • Menu Slug - ID for this specific item.
  • Callback - same as top-level.
Plugin Pages: the add_plugins_page function was made to make it easier for plugin developers: http://codex.wordpress.org/Function_Reference/add_plugins_page
add_plugins_page(Page Title, Menu Title, Capability, Menu Slug, Callback);
All of its parameters work the same as add_menu_page.  This should be called from within your plugin's php file and not your functions.php.

Theme Pages: uses add_theme_page to add a submenu to the appearance menu: http://codex.wordpress.org/Function_Reference/add_theme_page
add_theme_page(Page Title, Menu Title, Capability, Menu Slug, Callback);
All of its parameters work the same as add_submenu_page.

Now what is each menu type good for exactly?:
  • Top-Level Menus - for when you have a group of important settings that don't fit in other existing menus.
  • Submenus - either custom or already existing, should be put under a parent menu that works.
  • Plugin Menus - used for plugins and should used tabbed navigation if there's multiple option pages.
  • Theme Menus - used for theme options and follows the same deal with tabbed menus as plugin menus.  However, top-level menus are sometimes used for theme options instead.
So that's everything I took down for part 3.  Here's the code I'm left with at the end of that: http://pastebin.com/aYk6kjzX

Part 4 is supposed to go more into detail about theme options so I'm starting that now but that's all for this post.

No comments:

Post a Comment