Using WordPress ‘post_format_meta_box()’ PHP function

The post_format_meta_box() WordPress PHP function displays post format form elements.

Usage

post_format_meta_box($post, $box);

Parameters

  • $post (WP_Post, required): The current post object.
  • $box (array, required): Post formats meta box arguments. Includes:
  • id (string): Meta box ‘id’ attribute.
  • title (string): Meta box title.
  • callback (callable): Meta box display callback.
  • args (array): Extra meta box arguments.

More information

See WordPress Developer Resources: post_format_meta_box

Examples

Display a custom post format meta box

This example demonstrates how to display a custom post format meta box for a post.

// Create a custom meta box
function custom_post_format_meta_box() {
add_meta_box(
'custom_post_format', // Meta box ID
'Custom Post Format', // Meta box title
'custom_post_format_callback', // Callback function
'post', // Post type
'side', // Context
'default' // Priority
);
}
add_action('add_meta_boxes', 'custom_post_format_meta_box');

// Display the custom post format meta box
function custom_post_format_callback($post) {
$format = get_post_format($post->ID);
if (false === $format) {
$format = 'standard';
}
post_format_meta_box($post, array('title' => 'Choose a custom post format'));
}

Save custom post format

This example demonstrates how to save the custom post format when the post is saved.

// Save custom post format
function save_custom_post_format($post_id) {
// Verify nonce
if (!isset($_POST['custom_post_format_nonce']) || !wp_verify_nonce($_POST['custom_post_format_nonce'], 'save_custom_post_format')) {
return;
}

// Save post format
if (isset($_POST['post_format'])) {
set_post_format($post_id, $_POST['post_format']);
}
}
add_action('save_post', 'save_custom_post_format');