• $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 ? ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    What is the value of $filename? It must be an absolute server path, not a URL (begins with something like /home/... and not https://...). 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_id has 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.

    Thread Starter pixel016

    (@pixel016)

    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.

    Thread Starter pixel016

    (@pixel016)

    Moderator bcworkz

    (@bcworkz)

    $filename is 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 $filename to the generate function, pass this:
    WP_CONTENT_DIR . '/uploads' . $filename

    Thread Starter pixel016

    (@pixel016)

    Ho i’m so heedless lol.

    Thank you so so much bcworkz for you help ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘wp_generate_attachment_metadata does not work’ is closed to new replies.