Adding a custom meta box to WordPress post editor using add_meta_box

The add_meta_box function in WordPress is used to add a meta box to the edit screen for a specific post type. This meta box can contain additional custom fields or content that is related to the post. It can be useful for allowing users to input and display additional information for a post, such as related links, images, or custom data.

By using the add_meta_box function, developers can customize the edit screen for a specific post type and provide a more tailored editing experience for users. This can help to streamline the content management process and make it easier for users to input and manage custom data for their posts.

Parameters accepted by the WordPress add_meta_box function

The add_meta_box function accepts the following parameters:

  • $id (string, required): Meta box ID (used in the ‘id’ attribute for the meta box).
  • $title (string, required): Title of the meta box.
  • $callback (callable, required): Function that fills the box with the desired content. The function should echo its output.
  • $screen (string array or WP_Screen, optional, default value: null): The screen or screens on which to show the box (such as a post type, ‘link’, or ‘comment’).
  • $context (string, optional, default value: ‘advanced’): The context within the screen where the box should display.
  • $priority (string, optional, default value: ‘default’): The priority within the context where the box should show. Accepts ‘high’, ‘core’, ‘default’, or ‘low’. Default ‘default’.
  • $callback_args (array, optional, default value: null): Data that should be set as the $args property of the box array (which is the second parameter passed to your callback).

Value returned by the function

The add_meta_box function does not return a value.

Examples

How to add a meta box to a post edit screen

Use the add_meta_box function to add a custom meta box to the post edit screen.

add_action( 'add_meta_boxes', 'prefix_add_custom_meta_box' );

function prefix_add_custom_meta_box() {
 add_meta_box(
 'custom-meta-box',
 'Custom Meta Box Title',
 'prefix_render_custom_meta_box',
 'post',
 'normal',
 'default'
 );
}

function prefix_render_custom_meta_box( $post ) {
 // Render meta box content
}

How to add a meta box to a custom post type

Use the add_meta_box function to add a custom meta box to a specific custom post type.

add_action( 'add_meta_boxes', 'prefix_add_custom_meta_box' );

function prefix_add_custom_meta_box() {
 add_meta_box(
 'custom-meta-box',
 'Custom Meta Box Title',
 'prefix_render_custom_meta_box',
 'custom_post_type',
 'normal',
 'default'
 );
}

function prefix_render_custom_meta_box( $post ) {
 // Render meta box content
}

How to save meta box data

Use the save_post hook to save the meta box data when the post is saved or updated.

add_action( 'save_post', 'prefix_save_custom_meta_box_data' );

function prefix_save_custom_meta_box_data( $post_id ) {
 // Check if our nonce is set
 if ( ! isset( $_POST['custom_meta_box_nonce'] ) ) {
 return;
 }

 // Verify that the nonce is valid
 if ( ! wp_verify_nonce( $_POST['custom_meta_box_nonce'], 'custom_meta_box_nonce' ) ) {
 return;
 }

 // Save meta box data
}

Conclusion

In conclusion, the add_meta_box function is an useful component for adding custom meta boxes to WordPress post edit screens. It allows developers to easily integrate additional content and functionality into the WordPress admin interface, providing a seamless user experience for content management.

By utilizing the add_meta_box function, developers can enhance the flexibility and customization of WordPress websites, opening up new possibilities for content organization and user interaction. With its straightforward syntax and extensive customization options, the add_meta_box function is a valuable asset for any WordPress developer looking to extend the capabilities of the platform.

The add_meta_box function is a key feature for WordPress development, offering a straightforward and efficient way to integrate custom meta boxes into the admin interface. Its versatility and ease of use make it a valuable tool for enhancing the functionality and user experience of WordPress websites.

Related WordPress Functions