Adding a Meta Box for Posts
We’ve learned how to add a meta box for admin pages created by the framework in the previous tutorials (Add a Meta Box in Admin Page, Add a Page Meta Box Specific to an In-page Tab ). Now let’s add a meta box for posts, meaning in post definition pages where you publish articles so that you can store custom meta field values associated with a post.
Steps
- Include the library
- Extend the library factory class
- Define form fields the setUp() method
- Instantiate the extended class
- Retrieve the meta box field values
Screenshot
Include the library
The library (framework) file needs to be loaded. Set your correct the library path here.
13 |
include( dirname( dirname( __FILE__ ) ) . '/library/apf/admin-page-framework.php' ); |
Extend the library class
We extend the AdminPageFramework_MetaBox factory class. For meta boxes for admin pages, we extended the AdminPageFramework_PageMetaBox class. The factory class names are slightly different.
1 2 3 |
class APF_Tutorial_SideMetaBox extends AdminPageFramework_MetaBox { // 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. To keep it simple, let’s just define one field.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
public function setUp() { /** * Adds setting fields in the meta box. */ $this->addSettingFields( array( 'field_id' => 'tutorial_normal_metabox_text', 'type' => 'text', 'title' => __( 'Text', 'admin-page-framework-tutorial' ), ) ); } |
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 post type slug to add the meta box to.
- (string) the context. Accepts either side, normal, or advanced.
- (string) the priority. Accepts either high, core, default or low.
For the third parameter, pass a value ‘post’ for the default post post type. For pages use ‘page’. See the codex for the slugs of the built-in post types
1 |
array( 'post’ ), |
You should have heard of custom post types. Custom post types are for creating contents like posts and pages but in a different section of the site. If you want to add a meta box for a custom post type, similarly, just set the post type slug in the third parameter of the custom post type.
1 |
array( ‘my_custom_post_type_slug’ ), |
68 69 70 71 72 73 74 |
new APF_Tutorial_SideMetaBox( null, // meta box ID - can be null. __( 'Tutorial - Add a Meta Box for Posts', 'admin-page-framework-demo' ), // title array( 'post' ), // post type slugs: post, page, etc. 'side', // context 'low' // priority ); |
Retrieve the meta box field values
Now how do we retrieve the stored meta value? It is stored in the postmeta table. So we can use the WordPress core get_post_meta() function. For that, we need to know the post ID as the meta data are associated with a post and a post ID is used to identify the association.
Let’s add a custom output at the bottom of a post content to see an example usage of it. First create a test post. Type a test string such as “Hello World!” in the editor. Also type something in the created meta box field as well. We are going to show this entered value in the front end.
In the front-end, the page should look like this without any modification. To get to the page, click on the View link which appears below the post title text field when you update the post.
Now insert the following function at the top of the APF_Tutorial_SideMetaBox class.
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 |
public function start() { add_action( 'the_content', array( $this, 'replyToAddContent' ) ); } /** * Called when a post content is displayed. * * @callback filter the_content * @return string */ public function replyToAddContent( $sContent ) { if ( ! is_singular() ) { return $sContent; } if ( ! is_main_query() ) { return $sContent; } global $post; if ( 'post' !== $post->post_type ) { return $sContent; } $_sMetaData = get_post_meta( $post->ID, 'tutorial_normal_metabox_text', true ); return $sContent . '<p>Admin Page Framework Tutorial Metabox Field: ' . $_sMetaData . '</p>'; } |
It will insert a custom content in the post article page with the the_content filter hook. The start() method is one of the pre-defined framework methods that is automatically called at the end of the constructor method (__consturct()). The difference between setUp() and start() is that start() will be called regardless of where the current page resides while setUp() is only called only in a post definition page of the admin area. In other words, the start() method can be called in the front-end.
Notice that in the callback method of the the_content filter hook, get_post_meta() function is used and the post ID is passed to the first parameter. In the second parameter, the field ID is set. What it means is that “give me the value of the field whose ID is ‘tutorial_normal_metabox_text’ associated with the displaying post.”
If you successfully add the code, the post article page in the front end will look like this.
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
<?php /* * Plugin Name: Admin Page Framework Tutorial 11 - Add a Meta Box for Posts * Plugin URI: http://en.michaeluno.jp/admin-page-framework * Description: Adds a meta box for posts. * 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__ ) ) . '/library/apf/admin-page-framework.php' ); class APF_Tutorial_SideMetaBox extends AdminPageFramework_MetaBox { public function start() { add_action( 'the_content', array( $this, 'replyToAddContent' ) ); } /** * Called when a post content is displayed. * * @callback filter the_content * @return string */ public function replyToAddContent( $sContent ) { if ( ! is_singular() ) { return $sContent; } if ( ! is_main_query() ) { return $sContent; } global $post; if ( 'post' !== $post->post_type ) { return $sContent; } $_sMetaData = get_post_meta( $post->ID, 'tutorial_normal_metabox_text', true ); return $sContent . '<p>Admin Page Framework Tutorial Metabox Field: ' . $_sMetaData . '</p>'; } /* * 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_metabox_text', 'type' => 'text', 'title' => __( 'Text', 'admin-page-framework-tutorial' ), ) ); } } new APF_Tutorial_SideMetaBox( null, // meta box ID - can be null. __( 'Tutorial - Add a Meta Box for Posts', 'admin-page-framework-demo' ), // title array( 'post' ), // post type slugs: post, page, etc. 'side', // context 'low' // priority ); |
Great tutorial, simply and easy to follow step by step.
Tanks