On this page
wp_get_attachment_metadata( int $attachment_id, bool $unfiltered = false ): array|false
Retrieves attachment metadata for attachment ID.
Parameters
$attachment_idint Required-
Attachment post ID. Defaults to global $post.
$unfilteredbool Optional-
If true, filters are not run.
Default:
false
Return
array|false Attachment metadata. False on failure.
widthintThe width of the attachment.heightintThe height of the attachment.filestringThe file path relative towp-content/uploads.sizesarrayKeys are size slugs, each value is an array containing'file','width','height', and'mime-type'.image_metaarrayImage metadata.filesizeintFile size of the attachment.
Source
File: wp-includes/post.php. View all references
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 );
}
Hooks
- apply_filters( 'wp_get_attachment_metadata',
array $data ,int $attachment_id ) -
Filters the attachment meta data.
Related
Uses
| Uses | Description |
|---|---|
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| get_post_meta() wp-includes/post.php | Retrieves a post meta field for the given post ID. |
| 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_img_tag_add_width_and_height_attr() wp-includes/media.php | Adds |
| wp_img_tag_add_srcset_and_sizes_attr() wp-includes/media.php | Adds |
| wp_get_original_image_path() wp-includes/post.php | Retrieves the path to an uploaded image file. |
| wp_get_original_image_url() wp-includes/post.php | Retrieves the URL to an original attachment image. |
| wp_get_missing_image_subsizes() wp-admin/includes/image.php | Compare the existing image sub-sizes (as saved in the attachment meta) to the currently registered image sub-sizes, and return the difference. |
| 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_Widget_Media_Image::render_media() wp-includes/widgets/class-wp-widget-media-image.php | Render the media on the frontend. |
| 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::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php | Prepares a single attachment output for response. |
| wp_get_attachment_image_srcset() wp-includes/media.php | Retrieves the value for an image attachment’s ‘srcset’ attribute. |
| wp_get_attachment_image_sizes() wp-includes/media.php | Retrieves the value for an image attachment’s ‘sizes’ attribute. |
| wp_calculate_image_sizes() wp-includes/media.php | Creates a ‘sizes’ attribute value for 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_image_editor() wp-admin/includes/image-edit.php | Loads the WP image-editing interface. |
| edit_form_image_editor() wp-admin/includes/media.php | Displays the image and editor in the post editor |
| attachment_submitbox_metadata() wp-admin/includes/media.php | Displays non-editable attachment metadata in the publish meta box. |
| get_media_item() wp-admin/includes/media.php | Retrieves HTML form for modifying the image attachment. |
| 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. |
| attachment_id3_data_meta_box() wp-admin/includes/meta-boxes.php | Displays fields for ID3 data. |
| Custom_Image_Header::step_2() wp-admin/includes/class-custom-image-header.php | Display second step of custom header image page. |
| get_uploaded_header_images() wp-includes/theme.php | Gets the header images uploaded for the active theme. |
| prepend_attachment() wp-includes/post-template.php | Wraps attachment in paragraph tag before content. |
| wp_maybe_generate_attachment_metadata() wp-includes/media.php | Maybe attempts to generate attachment metadata, if missing. |
| wp_prepare_attachment_for_js() wp-includes/media.php | Prepares an attachment post object for JS, where it is expected to be JSON-encoded and fit into an Attachment model. |
| gallery_shortcode() wp-includes/media.php | Builds the Gallery shortcode output. |
| wp_playlist_shortcode() wp-includes/media.php | Builds the Playlist shortcode output. |
| image_get_intermediate_size() wp-includes/media.php | Retrieves the image’s intermediate size (resized) path, width, and height. |
| wp_get_attachment_image() wp-includes/media.php | Gets an HTML img element representing an image attachment. |
| image_downsize() wp-includes/media.php | Scales an image to fit a particular size (such as ‘thumb’ or ‘medium’). |
| wp_delete_attachment() wp-includes/post.php | Trashes or deletes an attachment. |
| wp_get_attachment_thumb_file() wp-includes/deprecated.php | Retrieves thumbnail for an attachment. |
| wp_xmlrpc_server::_prepare_media_item() wp-includes/class-wp-xmlrpc-server.php | Prepares media item data for return in an XML-RPC object. |
Changelog
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_get_attachment_metadata