wp_maybe_generate_attachment_metadata

Maybe attempts to generate attachment metadata, if missing.

Description

wp_maybe_generate_attachment_metadata( (WP_Post) $attachment ); 

Parameters (1)

0. $attachment (WP_Post)
Attachment object.

Usage

  1. if ( !function_exists( 'wp_maybe_generate_attachment_metadata' ) ) { 
  2. require_once ABSPATH . WPINC . '/media.php'; 
  3.  
  4. // Attachment object. 
  5. $attachment = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_maybe_generate_attachment_metadata($attachment); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/media.php  
  1. function wp_maybe_generate_attachment_metadata( $attachment ) { 
  2. if ( empty( $attachment ) || ( empty( $attachment->ID ) || ! $attachment_id = (int) $attachment->ID ) ) { 
  3. return; 
  4.  
  5. $file = get_attached_file( $attachment_id ); 
  6. $meta = wp_get_attachment_metadata( $attachment_id ); 
  7. if ( empty( $meta ) && file_exists( $file ) ) { 
  8. $_meta = get_post_meta( $attachment_id ); 
  9. $regeneration_lock = 'wp_generating_att_' . $attachment_id; 
  10. if ( ! array_key_exists( '_wp_attachment_metadata', $_meta ) && ! get_transient( $regeneration_lock ) ) { 
  11. set_transient( $regeneration_lock, $file ); 
  12. wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) ); 
  13. delete_transient( $regeneration_lock );