wp_generate_attachment_metadata returns empty array

I believe the following is happening here:

  1. As @Luke pointed out, wp_insert_attachment() takes an array of post data. The format of the array you are passing to it is not correct. The keys are different. What should be stored as the post_mime_type, is being passed with the key type. Because of this, no mime type is being saved for the post.
  2. wp_generate_attachment_metadata() is looking for the mime type so it can generate the metadata based on that. But since the mime type wasn’t saved correctly it just returns an empty array.

The solution is to do $uploadedImage['post_mime_type'] = $uploadedImage['type'], or better yet use media_handle_upload(), which will handle the whole media upload for you.

Example based on your code:

foreach ( $_FILES as $key => $file ) {
    media_handle_upload( $key, 0, array(), $upload_overrides );
}

Leave a Comment