How to add custom meta boxes to WP’s edit post pages

Via your (child-)themes functions.php you can easily add custom meta boxes to posts, or even custom post types, edit pages. In this example I’ll show you how to add a custom meta box containing some information for the future authors on how to edit a post type called “books”.

At first we have to add the new meta box:

// Add meta boxes to post type book
function add_book_meta_boxes() {
    add_meta_box(
    'author_info',    // Identifier
    'Author Info',    // Heading
    'author_info_meta_box_content',    // Name of the function we'll use to display the content
    'book',    // Post type
    'normal',  // Where to place the box; "side" would be also possible
    'high'    // Priority; in this case "high" because we want the box to be placed at the top
  );

    // We could add more meta boxes if we need to
}
add_action( 'add_meta_boxes', 'add_book_meta_boxes' );

Now WordPress knows the new meta box, but we still have to define what should be displayed:

// Display content of meta box author_info
function author_info_meta_box_content() {
    echo 'These are some instructions on how to add new books...";
}

This is of course just a simple example. Possibilities are endless. I personally used it to display some ACF values recently, but you can even use it to display forms and retrieve new values.

 

Leave a Comment

Your email address will not be published. Required fields are marked *