add_meta_boxes

Home » Hooks » add_meta_boxes

WordPress hooks are a fundamental part of the platform’s architecture, allowing developers to extend and modify its functionality in countless ways. One of the most important hooks for customizing the admin area of a WordPress site is add_meta_boxes.

The add_meta_boxes hook is fired when the edit form for a post or page is about to be displayed in the WordPress admin area. This hook allows developers to add custom meta boxes to the post editor screen, which can contain additional fields or data for that specific post.

For example, if you’re building a recipe website, you might want to add a custom meta box to the post editor screen that lets the user input the ingredients and directions for that recipe. By using the add_meta_boxes hook, you can add this custom meta box to the editor screen and save the data entered by the user.

Here’s an example of how to use the add_meta_boxes hook to add a custom meta box to the post editor screen:

function wpse_recipe_meta_box() {
  add_meta_box(
    'recipe_meta_box',
    'Recipe Details',
    'wpse_recipe_meta_box_callback',
    'post',
    'normal',
    'high'
  );
}
add_action( 'add_meta_boxes', 'wpse_recipe_meta_box' );

function wpse_recipe_meta_box_callback( $post ) {
  // Output the HTML for the meta box
}

In this example, we’re using the add_meta_box function inside the wpse_recipe_meta_box function to create a new meta box with the title "Recipe Details". We’re then specifying that this meta box should only be displayed on the post editor screen, not the page editor screen. Finally, we’re passing in a callback function (wpse_recipe_meta_box_callback) that will be responsible for rendering the contents of the meta box.

With this code in place, WordPress will call our wpse_recipe_meta_box callback function whenever the add_meta_boxes hook is fired, giving us the opportunity to add our custom meta box to the post editor screen.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.