How to Create Custom WP Metabox

Default and custom types already have default fields that we can fill in, but what it we need custom data for these post types? In this kind of need, we can opt to create custom metaboxes which will contain custom fields for the desired post types.

To make the implementation of a custom metabox easier, I created an abtract class that you can find here.

So, to create a custom metabox, simply follow the steps below:

Step 1: Copy theĀ Metabox Class from WDT-Framework

Step 2: Extend the class and defined the needed functons:

  • _showForm() – this is where you should define the content of your metabox (normally a set of custom fields)
    public function _showForm($post) {
    	$value = get_post_meta($post->ID, 'reference', true);
        	<input type="text" name="reference" value="<?php echo $value ?>" />
  • _saveMetadata() – this is where you should define how the fields are saved.
    protected function _saveMetadata($post_id, $user_inputs) {
        //OK, it's safe for us to save the data now.
        update_post_meta($post_id, 'reference', $user_inputs['reference']);

Step 3: Instantiate your class.

(string) the id of your metabox; must be unique

(string) the title if your metabox

(array) what screens should the metabox appear; normally an array of post types

(string) options: normal, side, advanced

(string) options: high, low, default


new PostExtraOptions('opt1', 'mymetabox', array('post', 'page'), 'side', 'default');
new PostExtraOptions('opt2', 'mymetabox', array(page'), 'normal', 'high');


Leave a Reply