wp_update_attachment_metadata()
Updates attachment (media file) metadata.
Metadata is recorded for each media file. They are located in the table wp_postmeta under the key _wp_attachment_metadata as a serialized array.
The data being updated - is an array of information about the file. If you need to update a specific element of the array, first retrieve the existing data using wp_get_attachment_metadata(), modify the retrieved array and save the data.
Hooks from the function
Returns
Int|true|false. Boolean. true if the data was updated or false.
Usage
wp_update_attachment_metadata( $post_id, $data );
- $post_id(int) (required)
- ID of the media file (attachment) whose metadata should be updated.
- $data(array) (required)
- The data that will be written to replace the existing data.
Examples
#1 Update the image caption
Let's add our data to the image with ID 656. To do this, first get the metadata, change it (add your own data), and save it:
$attach_id = 656; $data = wp_get_attachment_metadata( $attach_id ); // set the custom data $data['image_meta']['my_data'] = 'This (string) of data I will need'; // update data $up = wp_update_attachment_metadata( $attach_id, $data ); echo $up ? 'Updated' : 'Not updated';
The $data was equal to following array when was updating:
Array ( [width] => 356 [height] => 299 [file] => 2011/05/dinamic-archives.png [sizes] => Array ( [thumbnail] => Array ( [file] => dinamic-archives-80x80.png [width] => 80 [height] => 80 [mime-type] => image/png ) [medium] => Array ( [file] => dinamic-archives-120x100.png [width] => 120 [height] => 100 [mime-type] => image/png ) ) [image_meta] => Array ( [aperture] => 0 [credit] => [camera] => [caption] => [created_timestamp] => 0 [copyright] => [focal_length] => 0 [iso] => 0 [shutter_speed] => 0 [title] => [my_data] => This (string) of data I will need ) )
Changelog
| Since 2.1.0 | Introduced. |