Create a Page Group

Page Group

Create a Page Group

When you open the dashboard in the admin area, you see the menu in the side bar. There are built-in top-level menu items such as Dashboard, Posts, Media Pages and so on and sub-menu pages belong to them. We’ve covered how to add a sub-menu page to one of the existing top-level pages. With Admin Page Framework, you can create your own set of pages including a top-level page and sub-menu pages as a page group.

Steps

  1. Include the library
  2. Extend the library class
  3. Define the setUp() method
  4. In the setUp method, define the sub-pages.
  5. Instantiate the extended class

Screenshot

Create a Page Group

Include the library

The library file needs to be loaded. Set your correct path.

Extend the library class

This time, let’s use APF_CreatePageGroup for the class name.

Define the setUp() method

In the setUp() method, we define the admin pages to be created. For that, we need to decide:

  1. the root page.
  2. the sub page title.
  3. the sub page slug.

We’ve covered this already in the previous tutorial.

In order to create a custom root page, we need to pass a string that is not listed below.

  • Dashboard
  • Posts
  • Media
  • Links
  • Pages
  • Comments
  • Appearance
  • Plugins
  • Users
  • Tools
  • Settings
  • Network Admin

This time, we are going to set “3. My Page Group” as the root page title and for that it needs to be passed to the first parameter of the setRootMenuPage() method . As it’s not the built-in menu item, the framework  automatically creates a new root menu page. The slug for the created root menu page will be the extended class name and stored internally.

To set the menu  icon placed in the sidebar menu, pass the url or the path of the icon to the second  parameter. The icon size should be 16 by 16 in pixel.

To add sub menu pages, we use the addSubMenuItems() method. We pass a sub-menu item definition array. It accepts sub-pages and external links. For sub-pages, give the title and page_slug arguments and for external links, give the title and the href argument. The screen_icon argument is optional and it is for WordPress v3.7.x or below. v3.8 or above does not support screen icons displayed along with the page title any more. If your plugin or theme should support older versions of WordPress, set a screen icon for each page.

Use Pre-defined Filters

So far, for callback methods, we just used do_ + page slug action hook. Let’s try another one: content_ + page slug. This is a filter, not an action hook. On contrary to action hooks, filters need to return the filtered value. In the below example code, notice that the function name is content_ + page slug and a string value is returned with appended modified text. This inserts the appended text into the page whose slug is my_first_page.

Like so, we can add more callback methods as necessary.

Instantiate the extended class

Finally, we instantiate the class. Unless it is instantiated, it will do nothing.

Code

Try the code below and post comments if you get a problem.

Leave a Reply

Your email address will not be published. Required fields are marked *