• Resolved chcw

    (@chcw)


    I am using the following code to insert an image as a post attachment. The code works OK, except that wp_update_attachment_metadata will always return false. Based on the document https://developer.www.ads-software.com/reference/functions/wp_update_attachment_metadata/, false indicate the post is invalid. However, I check the media library and can see the image is added properly with all the required info, also different dimension versions are generated in the upload dir, so I cannot finger out why wp_update_attachment_metadata returns false.

    <?php 
    // $filename should be the path to a file in the upload directory.
    $filename = '/path/to/uploads/2013/03/filename.jpg';
    
    // The ID of the post this attachment is for.
    $parent_post_id = 37;
    
    // Check the type of file. We'll use this as the 'post_mime_type'.
    $filetype = wp_check_filetype( basename( $filename ), null );
    
    // Get the path to the upload directory.
    $wp_upload_dir = wp_upload_dir();
    
    // Prepare an array of post data for the attachment.
    $attachment = array(
        'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ), 
        'post_mime_type' => $filetype['type'],
        'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
        'post_content'   => '',
        'post_status'    => 'inherit'
    );
    
    // Insert the attachment.
    $attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
    
    // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    
    // Generate the metadata for the attachment, and update the database record.
    $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
    // $result is always false
    $result = wp_update_attachment_metadata( $attach_id, $attach_data );
    
    set_post_thumbnail( $parent_post_id, $attach_id );
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    You can get a false return because the update data is the same as what is already in place. This can happen when the call to update_post_meta() returns false. Another possible cause is that getting the attachment post failed due to a race condition. Eventually the post will be properly in place, but the get_post() call executed too soon after initial insertion.

    It’s not clear what causes your code to execute, but it could possibly execute more than once for the same file if it’s hooked to certain actions. The second time through, nothing had changed so false is returned.

    If you look at the source code for wp_generate_attachment_metadata(), when it calls wp_update_attachment_metadata() (line 581) it doesn’t bother to check the return value, perhaps because it can be misleading.

    Thread Starter chcw

    (@chcw)

    @bcworkz

    I see. Thank you very much.

    • This reply was modified 7 months, 2 weeks ago by chcw.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘wp_update_attachment_metadata always return false’ is closed to new replies.