Using WordPress ‘attachment_submitbox_metadata()’ PHP function

The attachment_submitbox_metadata() WordPress PHP function displays non-editable attachment metadata in the publish meta box.

Usage

This function does not take any parameters, so it is very straightforward to use:

attachment_submitbox_metadata();

Parameters

This function does not take any parameters.

More information

See WordPress Developer Resources: attachment_submitbox_metadata()

This function does not have a deprecated version and is usually used in admin-side WordPress development.

Examples

Displaying Attachment Metadata

The following code will display the metadata of an attachment in the publish meta box.

// When we are in the edit post screen
if ( 'post' == $typenow ) {
    // Display the attachment metadata
    attachment_submitbox_metadata();
}

Displaying Metadata on a Custom Post Type

Let’s say you have a custom post type called “portfolio” and you want to display the attachment metadata.

// When we are in the edit portfolio screen
if ( 'portfolio' == $typenow ) {
    // Display the attachment metadata
    attachment_submitbox_metadata();
}

Displaying Metadata on All Post Types

If you want to display the attachment metadata on all post types, you can do this:

// No matter what the post type is
attachment_submitbox_metadata();

Conditional Metadata Display

You may want to display the attachment metadata conditionally, based on a certain property of the post. For example, you might only want to display it for posts in a certain category.

// Get the post's categories
$categories = get_the_category();

// Check if the post is in the 'special' category
foreach ( $categories as $category ) {
    if ( 'special' == $category->slug ) {
        // Display the attachment metadata
        attachment_submitbox_metadata();
    }
}

Using with Action Hooks

This function is often used with action hooks to display the metadata at the right time and place.

// Add an action to 'post_submitbox_misc_actions' hook
add_action( 'post_submitbox_misc_actions', 'my_custom_function' );

function my_custom_function() {
    // Display the attachment metadata
    attachment_submitbox_metadata();
}

In this example, the function my_custom_function() is hooked to the post_submitbox_misc_actions action hook. This means it will run when WordPress is about to display the misc actions in the post submit box.