Custom Meta Box in WordPress

Although it might seem a little scary at first, adding a custom meta box to the back-end of a WordPress site is actually very simple.

If you want to know more about meta boxes, check out the WordPress meta box documentation.

//---- Register meta boxes
function register_meta_boxes($post) {
    // Will only show on posts, below the editor
    add_meta_box('meta_box_1', 'Test', 'build_meta_box', 'post', 'normal', 'low');

    // Will only show on pages, in the sidebar
    add_meta_box('meta_box_2', 'Test', 'build_meta_box', 'page', 'side', 'low');
}

add_action('add_meta_boxes', 'register_meta_boxes');

//---- Output of meta box
function build_meta_box($post) {
    echo 'Some simple text!';
}

Note: You’ll see in the build_meta_box() function we have access to the $post variable, from this we can grab anything we want about the post, like the title or custom post meta.

Inspirational Newsletter


Join the newsletter to get the best articles, tutorials and exclusive freebies every two weeks.

No spam. You can unsubscribe at any time.