Using WordPress ‘media_meta’ PHP filter

The media_meta WordPress PHP filter allows you to modify the media metadata displayed in the attachment’s details.

Usage

add_filter('media_meta', 'your_custom_function', 10, 2);

function your_custom_function($media_dims, $post) {
    // your custom code here

    return $media_dims;
}

Parameters

  • $media_dims (string) – The HTML markup containing the media dimensions.
  • $post (WP_Post) – The WP_Post attachment object.

More information

See WordPress Developer Resources: media_meta

Examples

Modify dimensions text

Customize the dimensions text displayed on the attachment’s details.

add_filter('media_meta', 'custom_media_dimensions_text', 10, 2);

function custom_media_dimensions_text($media_dims, $post) {
    $media_dims = str_replace('Dimensions:', 'Size:', $media_dims);
    return $media_dims;
}

Hide dimensions for specific file types

Hide the dimensions for all PDF attachments.

add_filter('media_meta', 'hide_dimensions_for_pdf', 10, 2);

function hide_dimensions_for_pdf($media_dims, $post) {
    if ($post->post_mime_type === 'application/pdf') {
        return '';
    }
    return $media_dims;
}

Add custom metadata

Add a custom metadata field to the attachment’s details.

add_filter('media_meta', 'add_custom_metadata', 10, 2);

function add_custom_metadata($media_dims, $post) {
    $custom_data = get_post_meta($post->ID, 'custom_data', true);
    $media_dims .= '<div class="custom-data">Custom Data: ' . $custom_data . '</div>';
    return $media_dims;
}

Display aspect ratio

Display the aspect ratio for image attachments.

add_filter('media_meta', 'show_aspect_ratio', 10, 2);

function show_aspect_ratio($media_dims, $post) {
    if (wp_attachment_is_image($post->ID)) {
        $metadata = wp_get_attachment_metadata($post->ID);
        $aspect_ratio = $metadata['width'] / $metadata['height'];
        $media_dims .= '<div class="aspect-ratio">Aspect Ratio: ' . round($aspect_ratio, 2) . '</div>';
    }
    return $media_dims;
}

Display file size

Display the file size for all attachments.

add_filter('media_meta', 'show_file_size', 10, 2);

function show_file_size($media_dims, $post) {
    $file_size = filesize(get_attached_file($post->ID));
    $media_dims .= '<div class="file-size">File Size: ' . size_format($file_size) . '</div>';
    return $media_dims;
}