On this page
wp_maybe_generate_attachment_metadata( WP_Post $attachment )
Maybe attempts to generate attachment metadata, if missing.
Parameters
$attachmentWP_Post Required-
Attachment object.
Source
File: wp-includes/media.php. View all references
function wp_maybe_generate_attachment_metadata( $attachment ) {
if ( empty( $attachment ) || empty( $attachment->ID ) ) {
return;
}
$attachment_id = (int) $attachment->ID;
$file = get_attached_file( $attachment_id );
$meta = wp_get_attachment_metadata( $attachment_id );
if ( empty( $meta ) && file_exists( $file ) ) {
$_meta = get_post_meta( $attachment_id );
$_lock = 'wp_generating_att_' . $attachment_id;
if ( ! array_key_exists( '_wp_attachment_metadata', $_meta ) && ! get_transient( $_lock ) ) {
set_transient( $_lock, $file );
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
delete_transient( $_lock );
}
}
}
Related
Uses
| Uses | Description |
|---|---|
| wp_generate_attachment_metadata() wp-admin/includes/image.php | Generates attachment meta data and create image sub-sizes for images. |
| get_transient() wp-includes/option.php | Retrieves the value of a transient. |
| set_transient() wp-includes/option.php | Sets/updates the value of a transient. |
| delete_transient() wp-includes/option.php | Deletes a transient. |
| wp_get_attachment_metadata() wp-includes/post.php | Retrieves attachment metadata for attachment ID. |
| wp_update_attachment_metadata() wp-includes/post.php | Updates metadata for an attachment. |
| get_attached_file() wp-includes/post.php | Retrieves attached file path based on attachment ID. |
| get_post_meta() wp-includes/post.php | Retrieves a post meta field for the given post ID. |
Used By
| Used By | Description |
|---|---|
| edit_form_image_editor() wp-admin/includes/media.php | Displays the image and editor in the post editor |
Changelog
| Version | Description |
|---|---|
| 3.9.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_maybe_generate_attachment_metadata