Using WordPress ‘attachment_submit_meta_box()’ PHP function

The attachment_submit_meta_box() WordPress PHP function displays attachment submit form fields. It uses the current post object as the parameter.

Usage

Let’s say we have an existing post object $my_post. This is how we could use the attachment_submit_meta_box() function:

attachment_submit_meta_box($my_post);

Parameters

  • $post (WP_Post Required): This is the current post object that the function uses.

More information

See WordPress Developer Resources: attachment_submit_meta_box()

Examples

Display the Submit Button for an Attachment

This example displays a submit button for a specific attachment.

$post = get_post($attachment_id); // Get the current post object
attachment_submit_meta_box($post); // Display the submit button

Update an Attachment’s Meta Box

This example updates an attachment’s meta box.

$post = get_post($attachment_id); // Get the current post object
attachment_submit_meta_box($post); // Update the meta box

Display Meta Box for New Attachment

This example displays a meta box for a newly created attachment.

$new_post = array(
  'post_title' => 'My New Attachment',
  'post_status' => 'publish',
  'post_type' => 'attachment'
);
$post_id = wp_insert_post($new_post); // Insert a new post
$post = get_post($post_id); // Get the new post object
attachment_submit_meta_box($post); // Display the meta box for the new post

Use with Custom Post Type

This example uses the function with a custom post type.

$new_post = array(
  'post_title' => 'My New Custom Type Post',
  'post_status' => 'publish',
  'post_type' => 'my_custom_type'
);
$post_id = wp_insert_post($new_post); // Insert a new custom type post
$post = get_post($post_id); // Get the new post object
attachment_submit_meta_box($post); // Display the meta box for the new custom type post

Display Meta Box for All Attachments

This example loops through all attachments and displays their respective meta box.

$args = array(
  'post_type' => 'attachment',
  'numberposts' => -1
);
$attachments = get_posts($args); // Get all attachments
foreach($attachments as $post) {
  setup_postdata($post); // Set up post data for each attachment
  attachment_submit_meta_box($post); // Display the meta box for each attachment
}