Add a Meta Box to an In-page Tab

Adding a Meta Box to a Specific In-page Tab

You may have noticed that the page meta box created in the previous tutorial appears in all the in-page tabs which belongs to the page whose slug is ‘my_tabs’. Is it possible to show a meta box only in a specific tab? Yes, you just need to pass an array defining the tabs to the third parameter of the factory class constructor.

Steps

  1. Prepare an admin page
  2. Include the library
  3. Extend the library class
  4. Define form fields the setUp() method
  5. Instantiate the extended class

Screenshot

Admin Page Framework Tutorial 10 Add a Page Meta Box in an In-page Tab

Prepare an admin page

Install and activate the plugin introduced in the tutorial, Create In-page Tabs and Add a Meta Box in Admin Page. We are going to write a plugin that works with them.

Include the library

Extend the library class

We extend the AdminPageFramework_PageMetaBox factory class. We’ve covered this already in the previous tutorial.

Define form fields in the setUp() method

In the setUp() method, we define form fields. We’ve covered this already as well.

Instantiate the extended class

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

Set the parameters as follows.

  1. (sting|null) the HTML ID selector for the meta box container element. Set null to generate it automatically.
  2. (sting) the meta box readable title shown at the top of the meta box.
  3. (string|array) the page slug to add the meta box.
  4. (string) the context. Accepts either side, normal, or advanced.
  5. (string) the priority. Accepts either high, core, default or low.

This time pay extra attention to the third parameter.

The array key represents the page slug and the value holds the in-page tab slugs in an array. Here only ‘my_tab_b’ is set. If you like to add another tab that belongs to the same page, pass it in the next element like this

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 *