W3cubDocs

/WordPress

wp_get_attachment_metadata( int $attachment_id, bool $unfiltered = false ): array|false

Retrieves attachment metadata for attachment ID.

Parameters

$attachment_idintrequired
Attachment post ID. Defaults to global $post.
$unfilteredbooloptional
If true, filters are not run.

Default: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.
  • filesize int
    File size of the attachment.

Source

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

apply_filters( ‘wp_get_attachment_metadata’, array $data, int $attachment_id )

Filters the attachment meta data.

Changelog

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

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