Add Metabox To Any Post Type

So you want to up your game and add a little bit of complexity to your post or custom post type perhaps. Meta boxes might be one of the things that you are looking for.

Lets start by creating a hook to the add_meta_boxes action.

//add the action
add_action( 'add_meta_boxes', 'custom_meta_boxes', 10, 2 );

Then create the callback function of the action, this is where you can check the current post-type and decide what to do. The defaults are post and page.

function custom_meta_boxes($post_type, $post){
    if($post_type == '{post-type}'){
        //now we create the meta box and add a callback function to render
        add_meta_box( 'my-meta-box',
                      'Meta Box Title',
                      'render_meta_box', //<-- callback function
                      'post-type',
                      'normal',
                      'default');
    }
}

Lastly you need to render the content of the meta box

//this will be called to render the meta box
function render_meta_box($post){
    //render the meta box here
}

Amiel Simbahon

Amiel Simbahon
Software Maker

Option Page for your Plugin or Theme

How to add option page to your plugin or theme. Continue reading

Wordpress Ajax Frontend

Published on February 21, 2017

Human Readable Filesize

Published on February 21, 2017