W3cubDocs

/WordPress

wp_get_attachment_metadata( int $attachment_id, bool $unfiltered = false )

Retrieves attachment metadata for attachment ID.

Parameters

$attachment_id

(int) (Required) Attachment post ID. Defaults to global $post.

$unfiltered

(bool) (Optional) If true, filters are not run.

Default value: false

Return

(array|false) Attachment metadata. False on failure.

  • 'width'
    (int) The width of the attachment.
  • 'height'
    (int) The height of the attachment.
  • 'file'
    (string) The file path relative to wp-content/uploads.
  • 'sizes'
    (array) Keys are size slugs, each value is an array containing 'file', 'width', 'height', and 'mime-type'.
  • 'image_meta'
    (array) Image metadata.

Source

File: wp-includes/post.php

function wp_get_attachment_metadata( $attachment_id = 0, $unfiltered = false ) {
	$attachment_id = (int) $attachment_id;

	$post = get_post( $attachment_id );
	if ( ! $post ) {
		return false;
	}

	$data = get_post_meta( $post->ID, '_wp_attachment_metadata', true );

	if ( $unfiltered ) {
		return $data;
	}

	/**
	 * Filters the attachment meta data.
	 *
	 * @since 2.1.0
	 *
	 * @param array|bool $data          Array of meta data for the given attachment, or false
	 *                                  if the object does not exist.
	 * @param int        $attachment_id Attachment post ID.
	 */
	return apply_filters( 'wp_get_attachment_metadata', $data, $post->ID );
}

Changelog

Version Description
2.1.0 Introduced.

© 2003–2019 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_get_attachment_metadata