Problem with wp_generate_attachment_metadata
-
I have searched far and wide, but have not found a solution. I have a real estate ad insertion form, I need to be able to upload photos of the listings. I can upload the photos to the server via ajax, return the filenames in a textarea and always via ajax, after submitting the form, I can upload the photos to wordpress and attach them to the ad The only problem is that it does not generate the photo metadata, wp_generate_attachment_metadata always returns an empty array. I can’t find a solution about it. I have another plugin with a similar form, but there I post the form not via ajax, but with the action = “post”, and I can safely generate the metadata.
This is the code with which I insert the attachments and link them to the newly created post.
//$filename = domoria-torino-strada-della-fornace-druento-15.jpg getted from the iterated array generated from textarea if ($filename != '') { $wp_upload_dir = wp_upload_dir(); $filename_path = $wp_upload_dir['path'] .'/'. $filename; $filename_url = $wp_upload_dir['url'] .'/'. $filename; $guid = $wp_upload_dir['url'] . '/' . basename( $filename_path ); $attachment = array( 'guid'=> $guid, 'post_mime_type' => 'image/jpeg', 'post_title' => $filename, 'post_content' => '', 'post_status' => 'inherit', 'post_parent' => $post_id ); $attach_id = wp_insert_attachment( $attachment, $filename_path); if($iter === 0){ set_post_thumbnail( $post_id, $attach_id ); } $ids [] = $attach_id; //this array needs for an ACF field //$filename_path (string) = "/home/uxo80ef6/domains/homeprime.sviluppo.host/public_html/wp-content/uploads/2021/12/domoria-torino-strada-della-fornace-druento-15.jpg" //$attach_id = 629 $file_uploaded_path = get_attached_file($attach_id); require_once( ABSPATH . 'wp-admin/includes/image.php' ); require_once( ABSPATH . 'wp-admin/includes/file.php' ); require_once( ABSPATH . 'wp-admin/includes/media.php' ); $attach_data = wp_generate_attachment_metadata( $attach_id, $file_uploaded_path ); wp_update_attachment_metadata( $attach_id, $attach_data ); $iter++; }
- The topic ‘Problem with wp_generate_attachment_metadata’ is closed to new replies.