wp_generate_attachment_metadata does not work
-
$filename = get_user_meta($userID, 'image_profil',true); $wp_filetype = wp_check_filetype(basename($filename), null ); $wp_upload_dir = wp_upload_dir(); $attachment = array( 'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment( $attachment, $filename); require_once( ABSPATH . 'wp-admin/includes/image.php' ); $attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); wp_update_attachment_metadata( $attach_id, $attach_data ); set_post_thumbnail( $post_id, $attach_id );Why the $attach_data return an empty array ? 🙁
-
What is the value of
$filename? It must be an absolute server path, not a URL (begins with something like/home/...and nothttp://...). The image must also reside in the uploads folder (so why the absolute path I’ve no idea). Only .jpg, .gif, .png image formats are supported.Also confirm that the attachment insertion was successful in that
$attach_idhas an actual post ID. The only other thing I can think of for failure is the uploads folders do not have the right permissions, but I assume you’ve been able to upload with the media uploader.Hi and tanks for your answer.
the value of $filename is : “/2014/01/4442451861_29185dfe82_b1-438×4381391070283.jpg”
I think attachment insertion work because i have my image on the media library (in wordpress admin). But this image has no dimension in the right block, and if I manually send an image from the administration so the same image will be many dimensions.
$filenameis a relative path, not absolute. I don’t know why the function wants an absolute path, but that’s what the Codex says.Instead of just passing
$filenameto the generate function, pass this:
WP_CONTENT_DIR . '/uploads' . $filenameHo i’m so heedless lol.
Thank you so so much bcworkz for you help 🙂
The topic ‘wp_generate_attachment_metadata does not work’ is closed to new replies.