wp_update_attachment_metadata() WordPress Function

The wp_update_attachment_metadata() function is used to update an attachment's metadata. This function should be called after an attachment is added or updated. The attachment's metadata is retrieved by passing the attachment's ID to the wp_get_attachment_metadata() function.

wp_update_attachment_metadata( int $attachment_id, array $data ) #

Updates metadata for an attachment.


Parameters

$attachment_id

(int)(Required)Attachment post ID.

$data

(array)(Required)Attachment meta data.


Top ↑

Return

(int|false) False if $post is invalid.


Top ↑

Source

File: wp-includes/post.php

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


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