wordpress / latest / functions / wp_get_attachment_metadata.html

wp_get_attachment_metadata( int $attachment_id, bool $unfiltered = false ): array|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: false

Return

array|false Attachment metadata. False on failure.

  • widthint
    The width of the attachment.
  • heightint
    The height of the attachment.
  • filestring
    The file path relative to wp-content/uploads.
  • sizesarray
    Keys are size slugs, each value is an array containing 'file', 'width', 'height', and 'mime-type'.
  • image_metaarray
    Image metadata.
  • filesizeint
    File size of the attachment.

Source

File: wp-includes/post.php. View all references

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

	if ( ! $attachment_id ) {
		$post = get_post();

		if ( ! $post ) {
			return false;
		}

		$attachment_id = $post->ID;
	}

	$data = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );

	if ( ! $data ) {
		return false;
	}

	if ( $unfiltered ) {
		return $data;
	}

	/**
	 * Filters the attachment meta data.
	 *
	 * @since 2.1.0
	 *
	 * @param array $data          Array of meta data for the given attachment.
	 * @param int   $attachment_id Attachment post ID.
	 */
	return apply_filters( 'wp_get_attachment_metadata', $data, $attachment_id );
}

Hooks

Uses

Uses Description

Used By

Used By Description

Changelog

Version Description
6.0.0 The $filesize value was added to the returned array.
2.1.0 Introduced.

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