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
- Prepare an admin page
- Include the library
- Extend the library class
- Define form fields the setUp() method
- Instantiate the extended class
Screenshot
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
13 |
include( dirname( dirname( __FILE__ ) ) . '/admin-page-framework/apf/admin-page-framework.php' ); |
Extend the library class
We extend the AdminPageFramework_PageMetaBox
factory class. We’ve covered this already in the previous tutorial.
1 2 3 |
class APF_Tutorial_NormalPageMetaBox extends AdminPageFramework_PageMetaBox { // our code goes here… } |
Define form fields in the setUp() method
In the setUp() method, we define form fields. We’ve covered this already as well.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
public function setUp() { /** * Adds setting fields in the meta box. */ $this->addSettingFields( array( 'field_id' => 'tutorial_normal_page_metabox_select', 'type' => 'select', 'title' => __( 'Select', 'admin-page-framework-tutorial' ), 'label' => array( 'a' => __( 'Apple', 'admin-page-framework-tutorial' ), 'b' => __( 'Banana', 'admin-page-framework-tutorial' ), 'c' => __( 'Cherry', 'admin-page-framework-tutorial' ), ), 'is_multiple' => true, 'attributes' => array( 'size' => 5, ), ), array( 'field_id' => 'tutorial_normal_page_metabox_textarea', 'type' => 'textarea', 'title' => __( 'Text Area', 'admin-page-framework-tutorial' ), 'repeatable' => true, ) ); |
Instantiate the extended class
Finally, we instantiate the class. Unless it is instantiated, it will do nothing.
Set the parameters as follows.
- (sting|null) the HTML ID selector for the meta box container element. Set null to generate it automatically.
- (sting) the meta box readable title shown at the top of the meta box.
- (string|array) the page slug to add the meta box.
- (string) the context. Accepts either side, normal, or advanced.
- (string) the priority. Accepts either high, core, default or low.
This time pay extra attention to the third parameter.
1 |
array( 'my_tabs' => array( 'my_tab_b' ) ), |
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
1 |
array( 'my_tabs' => array( 'my_tab_b', ‘my_tab_c’ ) ), |
52 53 54 55 56 57 58 |
new APF_Tutorial_NormalPageMetaBox( null, // meta box id - passing null will make it auto generate __( 'Normal Page Meta Box', 'admin-page-framework-tutorial' ), // title array( 'my_tabs' => array( 'my_tab_b' ) ), 'normal', // context 'default' // priority ); |
Code
Try the code below and post comments if you get a problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<?php /* * Plugin Name: Admin Page Framework Tutorial 10 - Add a Page Meta Box Specific to an In-page Tab * Plugin URI: http://en.michaeluno.jp/admin-page-framework * Description: Adds a page meta box specific to in-page tab. * Author: Michael Uno * Author URI: http://michaeluno.jp * Version: 1.0.1 * Requirements: PHP 5.2.4 or above, WordPress 3.4 or above. Admin Page Framework 3.0.6 or above */ // Include the library file. Set your file path here. include( dirname( dirname( __FILE__ ) ) . '/admin-page-framework/apf/admin-page-framework.php' ); class APF_Tutorial_NormalPageMetaBox extends AdminPageFramework_PageMetaBox { /* * Use the setUp() method to define settings of this meta box. */ public function setUp() { /** * Adds setting fields in the meta box. */ $this->addSettingFields( array( 'field_id' => 'tutorial_normal_page_metabox_select', 'type' => 'select', 'title' => __( 'Select', 'admin-page-framework-tutorial' ), 'label' => array( 'a' => __( 'Apple', 'admin-page-framework-tutorial' ), 'b' => __( 'Banana', 'admin-page-framework-tutorial' ), 'c' => __( 'Cherry', 'admin-page-framework-tutorial' ), ), 'is_multiple' => true, 'attributes' => array( 'size' => 5, ), ), array( 'field_id' => 'tutorial_normal_page_metabox_textarea', 'type' => 'textarea', 'title' => __( 'Text Area', 'admin-page-framework-tutorial' ), 'repeatable' => true, ) ); } } new APF_Tutorial_NormalPageMetaBox( null, // meta box id - passing null will make it auto generate __( 'Normal Page Meta Box', 'admin-page-framework-tutorial' ), // title array( 'my_tabs' => array( 'my_tab_b' ) ), 'normal', // context 'default' // priority ); |