wp_update_attachment_metadata

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

WordPress Version: 5.7

/**
 * Updates metadata for an attachment.
 *
 * @since 2.1.0
 *
 * @param int   $attachment_id Attachment post ID.
 * @param array $data          Attachment meta data.
 * @return int|false False if $post is invalid.
 */
function wp_update_attachment_metadata($attachment_id, $data)
{
    $attachment_id = (int) $attachment_id;
    $post = get_post($attachment_id);
    if (!$post) {
        return false;
    }
    /**
     * Filters the updated attachment meta data.
     *
     * @since 2.1.0
     *
     * @param array $data          Array of updated attachment meta data.
     * @param int   $attachment_id Attachment post ID.
     */
    $data = apply_filters('wp_update_attachment_metadata', $data, $post->ID);
    if ($data) {
        return update_post_meta($post->ID, '_wp_attachment_metadata', $data);
    } else {
        return delete_post_meta($post->ID, '_wp_attachment_metadata');
    }
}

WordPress Version: 5.5

/**
 * Updates metadata for an attachment.
 *
 * @since 2.1.0
 *
 * @param int   $attachment_id Attachment post ID.
 * @param array $data          Attachment meta data.
 * @return int|bool False if $post is invalid.
 */
function wp_update_attachment_metadata($attachment_id, $data)
{
    $attachment_id = (int) $attachment_id;
    $post = get_post($attachment_id);
    if (!$post) {
        return false;
    }
    /**
     * Filters the updated attachment meta data.
     *
     * @since 2.1.0
     *
     * @param array $data          Array of updated attachment meta data.
     * @param int   $attachment_id Attachment post ID.
     */
    $data = apply_filters('wp_update_attachment_metadata', $data, $post->ID);
    if ($data) {
        return update_post_meta($post->ID, '_wp_attachment_metadata', $data);
    } else {
        return delete_post_meta($post->ID, '_wp_attachment_metadata');
    }
}

WordPress Version: 5.3

/**
 * Update metadata for an attachment.
 *
 * @since 2.1.0
 *
 * @param int   $attachment_id Attachment post ID.
 * @param array $data          Attachment meta data.
 * @return int|bool False if $post is invalid.
 */
function wp_update_attachment_metadata($attachment_id, $data)
{
    $attachment_id = (int) $attachment_id;
    $post = get_post($attachment_id);
    if (!$post) {
        return false;
    }
    /**
     * Filters the updated attachment meta data.
     *
     * @since 2.1.0
     *
     * @param array $data          Array of updated attachment meta data.
     * @param int   $attachment_id Attachment post ID.
     */
    $data = apply_filters('wp_update_attachment_metadata', $data, $post->ID);
    if ($data) {
        return update_post_meta($post->ID, '_wp_attachment_metadata', $data);
    } else {
        return delete_post_meta($post->ID, '_wp_attachment_metadata');
    }
}

WordPress Version: 4.9

/**
 * Update metadata for an attachment.
 *
 * @since 2.1.0
 *
 * @param int   $attachment_id Attachment post ID.
 * @param array $data          Attachment meta data.
 * @return int|bool False if $post is invalid.
 */
function wp_update_attachment_metadata($attachment_id, $data)
{
    $attachment_id = (int) $attachment_id;
    if (!$post = get_post($attachment_id)) {
        return false;
    }
    /**
     * Filters the updated attachment meta data.
     *
     * @since 2.1.0
     *
     * @param array $data          Array of updated attachment meta data.
     * @param int   $attachment_id Attachment post ID.
     */
    if ($data = apply_filters('wp_update_attachment_metadata', $data, $post->ID)) {
        return update_post_meta($post->ID, '_wp_attachment_metadata', $data);
    } else {
        return delete_post_meta($post->ID, '_wp_attachment_metadata');
    }
}

WordPress Version: 4.6

/**
 * Update metadata for an attachment.
 *
 * @since 2.1.0
 *
 * @param int   $post_id Attachment ID.
 * @param array $data    Attachment data.
 * @return int|bool False if $post is invalid.
 */
function wp_update_attachment_metadata($post_id, $data)
{
    $post_id = (int) $post_id;
    if (!$post = get_post($post_id)) {
        return false;
    }
    /**
     * Filters the updated attachment meta data.
     *
     * @since 2.1.0
     *
     * @param array $data    Array of updated attachment meta data.
     * @param int   $post_id Attachment ID.
     */
    if ($data = apply_filters('wp_update_attachment_metadata', $data, $post->ID)) {
        return update_post_meta($post->ID, '_wp_attachment_metadata', $data);
    } else {
        return delete_post_meta($post->ID, '_wp_attachment_metadata');
    }
}

WordPress Version: 4.0

/**
 * Update metadata for an attachment.
 *
 * @since 2.1.0
 *
 * @param int   $post_id Attachment ID.
 * @param array $data    Attachment data.
 * @return int|bool False if $post is invalid.
 */
function wp_update_attachment_metadata($post_id, $data)
{
    $post_id = (int) $post_id;
    if (!$post = get_post($post_id)) {
        return false;
    }
    /**
     * Filter the updated attachment meta data.
     *
     * @since 2.1.0
     *
     * @param array $data    Array of updated attachment meta data.
     * @param int   $post_id Attachment ID.
     */
    if ($data = apply_filters('wp_update_attachment_metadata', $data, $post->ID)) {
        return update_post_meta($post->ID, '_wp_attachment_metadata', $data);
    } else {
        return delete_post_meta($post->ID, '_wp_attachment_metadata');
    }
}

WordPress Version: 3.9

/**
 * Update metadata for an attachment.
 *
 * @since 2.1.0
 *
 * @param int $post_id Attachment ID.
 * @param array $data Attachment data.
 * @return int
 */
function wp_update_attachment_metadata($post_id, $data)
{
    $post_id = (int) $post_id;
    if (!$post = get_post($post_id)) {
        return false;
    }
    /**
     * Filter the updated attachment meta data.
     *
     * @since 2.1.0
     *
     * @param array $data    Array of updated attachment meta data.
     * @param int   $post_id Attachment ID.
     */
    if ($data = apply_filters('wp_update_attachment_metadata', $data, $post->ID)) {
        return update_post_meta($post->ID, '_wp_attachment_metadata', $data);
    } else {
        return delete_post_meta($post->ID, '_wp_attachment_metadata');
    }
}

WordPress Version: 3.7

/**
 * Update metadata for an attachment.
 *
 * @since 2.1.0
 *
 * @param int $post_id Attachment ID.
 * @param array $data Attachment data.
 * @return int
 */
function wp_update_attachment_metadata($post_id, $data)
{
    $post_id = (int) $post_id;
    if (!$post = get_post($post_id)) {
        return false;
    }
    if ($data = apply_filters('wp_update_attachment_metadata', $data, $post->ID)) {
        return update_post_meta($post->ID, '_wp_attachment_metadata', $data);
    } else {
        return delete_post_meta($post->ID, '_wp_attachment_metadata');
    }
}