wp_get_attachment_metadata() WordPress Function

The wp_get_attachment_metadata() function is used to retrieve data about an attachment from the WordPress database. This data includes the file name, file type, file size, and other data about the attachment. This function can be used to retrieve data about an attachment that has been uploaded to the WordPress site.

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


Top ↑

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.


Top ↑

Source

File: wp-includes/post.php

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 );
}


Top ↑

Changelog

Changelog
VersionDescription
2.1.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More