1 comment

  1. You can try this one:
    if you image file has the meta data, you can use the following function to generate the data automatically.

    // Define attachment metadata 
    $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
    

    Find more about the variables the above function generates in the following link
    https://codex.wordpress.org/Function_Reference/wp_generate_attachment_metadata

    If the file does not have meta, You have form as an array manually like the following one.

    $attach_data = array(); 
    $attach_data["image_meta"]["caption"] ='';
    $attach_data["image_meta"]["title"] ='';
    

    You can get an idea about an image’s metadata variables in the following link.
    https://codex.wordpress.org/Function_Reference/wp_read_image_metadata#Return_Values

    Finally update the values as metadata to that attachment.

    // Assign metadata to attachment
    wp_update_attachment_metadata( $attachment_id, $attach_data );
    

Comments are closed.