wp_get_attachment_metadata

The timeline below displays how wordpress function wp_get_attachment_metadata has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Retrieves attachment metadata for attachment ID.
 *
 * @since 2.1.0
 * @since 6.0.0 The `$filesize` value was added to the returned array.
 *
 * @param int  $attachment_id Attachment post ID. Defaults to global $post.
 * @param bool $unfiltered    Optional. If true, filters are not run. Default false.
 * @return array|false {
 *     Attachment metadata. False on failure.
 *
 *     @type int    $width      The width of the attachment.
 *     @type int    $height     The height of the attachment.
 *     @type string $file       The file path relative to `wp-content/uploads`.
 *     @type array  $sizes      Keys are size slugs, each value is an array containing
 *                              'file', 'width', 'height', and 'mime-type'.
 *     @type array  $image_meta Image metadata.
 *     @type int    $filesize   File size of the attachment.
 * }
 */
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);
}

WordPress Version: 5.7

/**
 * Retrieves attachment metadata for attachment ID.
 *
 * @since 2.1.0
 *
 * @param int  $attachment_id Attachment post ID. Defaults to global $post.
 * @param bool $unfiltered    Optional. If true, filters are not run. Default false.
 * @return array|false {
 *     Attachment metadata. False on failure.
 *
 *     @type int    $width      The width of the attachment.
 *     @type int    $height     The height of the attachment.
 *     @type string $file       The file path relative to `wp-content/uploads`.
 *     @type array  $sizes      Keys are size slugs, each value is an array containing
 *                              'file', 'width', 'height', and 'mime-type'.
 *     @type array  $image_meta Image metadata.
 * }
 */
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);
}

WordPress Version: 6.1

/**
 * Retrieves attachment metadata for attachment ID.
 *
 * @since 2.1.0
 *
 * @param int  $attachment_id Attachment post ID. Defaults to global $post.
 * @param bool $unfiltered    Optional. If true, filters are not run. Default false.
 * @return array|false {
 *     Attachment metadata. False on failure.
 *
 *     @type int    $width      The width of the attachment.
 *     @type int    $height     The height of the attachment.
 *     @type string $file       The file path relative to `wp-content/uploads`.
 *     @type array  $sizes      Keys are size slugs, each value is an array containing
 *                              'file', 'width', 'height', and 'mime-type'.
 *     @type array  $image_meta Image metadata.
 * }
 */
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|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, $attachment_id);
}

WordPress Version: 5.6

/**
 * Retrieves attachment metadata for attachment ID.
 *
 * @since 2.1.0
 *
 * @param int  $attachment_id Attachment post ID. Defaults to global $post.
 * @param bool $unfiltered    Optional. If true, filters are not run. Default false.
 * @return array|false {
 *     Attachment metadata. False on failure.
 *
 *     @type int    $width      The width of the attachment.
 *     @type int    $height     The height of the attachment.
 *     @type string $file       The file path relative to `wp-content/uploads`.
 *     @type array  $sizes      Keys are size slugs, each value is an array containing
 *                              'file', 'width', 'height', and 'mime-type'.
 *     @type array  $image_meta Image metadata.
 * }
 */
function wp_get_attachment_metadata($attachment_id = 0, $unfiltered = false)
{
    $attachment_id = (int) $attachment_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|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, $attachment_id);
}

WordPress Version: 5.5

/**
 * Retrieves attachment metadata for attachment ID.
 *
 * @since 2.1.0
 *
 * @param int  $attachment_id Attachment post ID. Defaults to global $post.
 * @param bool $unfiltered    Optional. If true, filters are not run. Default false.
 * @return array|false {
 *     Attachment metadata. False on failure.
 *
 *     @type int    $width      The width of the attachment.
 *     @type int    $height     The height of the attachment.
 *     @type string $file       The file path relative to `wp-content/uploads`.
 *     @type array  $sizes      Keys are size slugs, each value is an array containing
 *                              'file', 'width', 'height', and 'mime-type'.
 *     @type array  $image_meta Image metadata.
 * }
 */
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);
}

WordPress Version: 5.3

/**
 * Retrieve attachment meta field for attachment ID.
 *
 * @since 2.1.0
 *
 * @param int  $attachment_id Attachment post ID. Defaults to global $post.
 * @param bool $unfiltered    Optional. If true, filters are not run. Default false.
 * @return mixed Attachment meta field. False on failure.
 */
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);
}

WordPress Version: 4.9

/**
 * Retrieve attachment meta field for attachment ID.
 *
 * @since 2.1.0
 *
 * @param int  $attachment_id Attachment post ID. Defaults to global $post.
 * @param bool $unfiltered    Optional. If true, filters are not run. Default false.
 * @return mixed Attachment meta field. False on failure.
 */
function wp_get_attachment_metadata($attachment_id = 0, $unfiltered = false)
{
    $attachment_id = (int) $attachment_id;
    if (!$post = get_post($attachment_id)) {
        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);
}

WordPress Version: 4.6

/**
 * Retrieve attachment meta field for attachment ID.
 *
 * @since 2.1.0
 *
 * @param int  $post_id    Attachment ID. Default 0.
 * @param bool $unfiltered Optional. If true, filters are not run. Default false.
 * @return mixed Attachment meta field. False on failure.
 */
function wp_get_attachment_metadata($post_id = 0, $unfiltered = false)
{
    $post_id = (int) $post_id;
    if (!$post = get_post($post_id)) {
        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        $post_id Attachment ID.
     */
    return apply_filters('wp_get_attachment_metadata', $data, $post->ID);
}

WordPress Version: 4.3

/**
 * Retrieve attachment meta field for attachment ID.
 *
 * @since 2.1.0
 *
 * @param int  $post_id    Attachment ID. Default 0.
 * @param bool $unfiltered Optional. If true, filters are not run. Default false.
 * @return mixed Attachment meta field. False on failure.
 */
function wp_get_attachment_metadata($post_id = 0, $unfiltered = false)
{
    $post_id = (int) $post_id;
    if (!$post = get_post($post_id)) {
        return false;
    }
    $data = get_post_meta($post->ID, '_wp_attachment_metadata', true);
    if ($unfiltered) {
        return $data;
    }
    /**
     * Filter 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        $post_id Attachment ID.
     */
    return apply_filters('wp_get_attachment_metadata', $data, $post->ID);
}

WordPress Version: 4.0

/**
 * Retrieve attachment meta field for attachment ID.
 *
 * @since 2.1.0
 *
 * @param int  $post_id    Attachment ID. Default 0.
 * @param bool $unfiltered Optional. If true, filters are not run. Default false.
 * @return string|bool Attachment meta field. False on failure.
 */
function wp_get_attachment_metadata($post_id = 0, $unfiltered = false)
{
    $post_id = (int) $post_id;
    if (!$post = get_post($post_id)) {
        return false;
    }
    $data = get_post_meta($post->ID, '_wp_attachment_metadata', true);
    if ($unfiltered) {
        return $data;
    }
    /**
     * Filter 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        $post_id Attachment ID.
     */
    return apply_filters('wp_get_attachment_metadata', $data, $post->ID);
}

WordPress Version: 3.9

/**
 * Retrieve attachment meta field for attachment ID.
 *
 * @since 2.1.0
 *
 * @param int $post_id Attachment ID
 * @param bool $unfiltered Optional, default is false. If true, filters are not run.
 * @return string|bool Attachment meta field. False on failure.
 */
function wp_get_attachment_metadata($post_id = 0, $unfiltered = false)
{
    $post_id = (int) $post_id;
    if (!$post = get_post($post_id)) {
        return false;
    }
    $data = get_post_meta($post->ID, '_wp_attachment_metadata', true);
    if ($unfiltered) {
        return $data;
    }
    /**
     * Filter 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        $post_id Attachment ID.
     */
    return apply_filters('wp_get_attachment_metadata', $data, $post->ID);
}

WordPress Version: 3.7

/**
 * Retrieve attachment meta field for attachment ID.
 *
 * @since 2.1.0
 *
 * @param int $post_id Attachment ID
 * @param bool $unfiltered Optional, default is false. If true, filters are not run.
 * @return string|bool Attachment meta field. False on failure.
 */
function wp_get_attachment_metadata($post_id = 0, $unfiltered = false)
{
    $post_id = (int) $post_id;
    if (!$post = get_post($post_id)) {
        return false;
    }
    $data = get_post_meta($post->ID, '_wp_attachment_metadata', true);
    if ($unfiltered) {
        return $data;
    }
    return apply_filters('wp_get_attachment_metadata', $data, $post->ID);
}