WordPress.org

Ready to get started?Download WordPress

Forums

upload and the_post_thumbnail( 'medium' ) (3 posts)

  1. funghi
    Member
    Posted 4 years ago #

    Hi everyone,
    I am developing a plugin that would allow a user to write new posts directly from the front end. Everything is fine, but I can't find any information on how to attach a uploaded file to the newly created post so that I could call the image this way: the_post_thumbnail( 'medium' ). (an image with the thumbnail is returned).
    The strange thing is that calling the_post_thumbnail( 'thumbnail' ) works just fine...

    Here is the code I wrote for the thumbail:


    $attachment_thumbnail_filename = image_resize($upload_preview["file"], get_option("thumbnail_size_w"), get_option("thumbnail_size_h"), true, get_option("thumbnail_size_w")."_".get_option("thumbnail_size_h"));</p>
    <p>$wp_filetype = wp_check_filetype(basename($attachment_thumbnail_filename), null );
    $attachment = array(
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => preg_replace('/\.[^.]+$/', '', basename($attachment_thumbnail_filename)),
    'post_content' => '',
    'post_type' => 'attachment',
    'post_status' => 'inherit'
    );</p>
    <p>$attach_id = wp_insert_attachment($attachment, $attachment_thumbnail_filename, $post_id );
    $attach_data = wp_generate_attachment_metadata( $attach_id, $attachment_thumbnail_filename );
    wp_update_attachment_metadata( $attach_id, $attach_data );
    update_post_meta( $post_id, '_thumbnail_id', $attach_id );

    Since this works fine to call the thumbnail, I thought that adapting the code for the medium size would work just fine! but not!

    Do you have any idee on where I could finde some ressources about this topic?
    thanks a lot for your help!

  2. goldenapples
    Member
    Posted 4 years ago #

    I just figured this one out - couldn't find any resources anywhere, but actually (in my case) it was incredibly simple. If users are uploading files that you want to save as attachments, don't bother going through all the headache of wp_insert_attachment() - there's a function in the wp-admin section that handles saving uploaded files in the correct directory, attaching them to a post, and generating all the intermediate sizes.

    Look at the function media_handle_upload() in wp-admin/includes/media.php. The function that ended up doing the trick for me was this:

    function insert_attachment($file_handler,$post_id,$setthumb='false') {
    
    		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_id = media_handle_upload( $file_handler, $post_id );
    
    		if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
    		return $attach_id;
    	}

    Hope this helps.

  3. Philip
    Member
    Posted 3 years ago #

    thanks for this @goldenapples,

    i came here after finding your post in your blog.
    i have a question about this,
    how you insert the image uploaded in the content of the post as a file?

    is it possible to return the post id and then update to attach the image(s)?

    i will really appreciate any help,
    thanks a lot,
    Philip

Topic Closed

This topic has been closed to new replies.

About this Topic