On this page
wp_update_attachment_metadata( int $attachment_id, array $data ): int|false
Updates metadata for an attachment.
Parameters
$attachment_idint Required-
Attachment post ID.
$dataarray Required-
Attachment meta data.
Return
int|false False if $post is invalid.
Source
File: wp-includes/post.php. View all references
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' );
}
}
Hooks
- apply_filters( 'wp_update_attachment_metadata',
array $data ,int $attachment_id ) -
Filters the updated attachment meta data.
Related
Uses
| Uses | Description |
|---|---|
| delete_post_meta() wp-includes/post.php | Deletes a post meta field for the given post ID. |
| update_post_meta() wp-includes/post.php | Updates a post meta field based on the given post ID. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
Used By
| Used By | Description |
|---|---|
| WP_REST_Attachments_Controller::edit_media_item() wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php | Applies edits to a media item and creates a new attachment record. |
| wp_update_image_subsizes() wp-admin/includes/image.php | If any of the currently registered image sub-sizes are missing, create them and update the image meta data. |
| wp_create_image_subsizes() wp-admin/includes/image.php | Creates image sub-sizes, adds the new data to the image meta |
| _wp_make_subsizes() wp-admin/includes/image.php | Low-level function to create image sub-sizes. |
| WP_Customize_Manager::import_theme_starter_content() wp-includes/class-wp-customize-manager.php | Imports theme starter content into the customized state. |
| WP_REST_Attachments_Controller::create_item() wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php | Creates a single attachment. |
| WP_Site_Icon::insert_attachment() wp-admin/includes/class-wp-site-icon.php | Inserts an attachment. |
| wp_ajax_crop_image() wp-admin/includes/ajax-actions.php | Ajax handler for cropping an image. |
| wp_restore_image() wp-admin/includes/image-edit.php | Restores the metadata for a given attachment. |
| wp_save_image() wp-admin/includes/image-edit.php | Saves image to post, along with enqueued changes in |
| wp_generate_attachment_metadata() wp-admin/includes/image.php | Generates attachment meta data and create image sub-sizes for images. |
| media_handle_upload() wp-admin/includes/media.php | Saves a file submitted from a POST request and create an attachment post for it. |
| media_handle_sideload() wp-admin/includes/media.php | Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload() . |
| edit_post() wp-admin/includes/post.php | Updates an existing post with values provided in |
| wp_ajax_save_attachment() wp-admin/includes/ajax-actions.php | Ajax handler for updating attachment attributes. |
| Custom_Image_Header::insert_attachment() wp-admin/includes/class-custom-image-header.php | Insert an attachment and its metadata. |
| Custom_Image_Header::step_2() wp-admin/includes/class-custom-image-header.php | Display second step of custom header image page. |
| Custom_Background::handle_upload() wp-admin/includes/class-custom-background.php | Handles an Image upload for the background image. |
| wp_maybe_generate_attachment_metadata() wp-includes/media.php | Maybe attempts to generate attachment metadata, if missing. |
| wp_xmlrpc_server::mw_newMediaObject() wp-includes/class-wp-xmlrpc-server.php | Uploads a file, following your settings. |
Changelog
| Version | Description |
|---|---|
| 2.1.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_update_attachment_metadata