'wp_handle_upload' Should Create the Resized Versions of Images

  1. jimmyink


    I'm really enjoying all the extra functionality that has come with custom post types but I'm still struggling to find a good solution/function for creating an image uploader that adds the images to the media library and resizes them to the 'thumbnail', 'medium' and 'large' sizes.

    The default media uploader is far to cumbersome and I need to work in the image upload process into my own UI.

    I'm currently using the 'wp_handle_upload' function followed by 'wp_insert_attachment' and then 'wp_update_attachment_metadata'. It works to an extent, my images get uploaded, but I'd expect any function that uploads an image to the media library should automatically handle the resizing. I'm trying to avoid plug-ins for this because I feel like they should be avoided when creating a custom post type.

    Posted: 7 years ago #
  2. jimmyink


    Ok, I've just found that the functions do exactly what I wanted them to do but I couldn't see the files in my ftp client (probably to do with the ftp/server user permissions). Hope this one day helps someone else who's losing their mind over this.

    Posted: 7 years ago #
  3. jevets

    Hey jimmyink, I AM currently losing my mind over this...

    I'm able to see the files in my uploads directory.

    When I upload an image via wp_handle_upload and wp_insert_attachment, WP isn't resizing to fit one of my custom preset sizes, added via add_image_size.

    I'm not using wp_update_attachment_metadata; is this the function responsible for generating different sizes?

    I've thrown it in there to test, but still no results.

    Any ideas? Thanks so much!

    Posted: 7 years ago #
  4. hello jimmyink
    Can you tell me how to use wp_handle_upload with wp_insert_attachment . I am making a plugin but not able to see attached image .Please tell me soon i am running at deadline.

    Thank you

    Posted: 7 years ago #
  5. jimmyink


    // Upload Image
    // 'input_name' is the name of the input field that you are targeting
    // e.g. (<input type="file" name="input_name"/>
    // This script will upload the file and save the id of the attachment to the post as meta, using the field name as the key.

    add_action('save_post', 'save_post_meta');

    function save_post_meta(){
    global $post;
    require_once( ABSPATH . 'wp-admin/includes/file.php' );

    foreach($_FILES as $field => $file){
    if($field == 'input_name' && $file['size'] > 1){
    $override['action'] = 'editpost';

    // Upload File
    $uploaded_file = wp_handle_upload($file, $override);
    $attachment = array(
    'post_title' => $file['name'],
    'post_content' => '',
    'post_type' => 'attachment',
    'post_parent' => $post->ID,
    'post_mime_type' => $file['type'],
    'guid' => $uploaded_file['url']

    // Create an Attachment in WordPress
    $id = wp_insert_attachment( $attachment,$uploaded_file[ 'file' ], $post->ID );
    wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $uploaded_file['file'] ) );

    update_post_meta($post->ID, $field, $id);

    //Remove it from the array to avoid duplicating during autosave/revisions.

    // Dont forget to include this jQuery to add the enctype and encoding attributes to the #post form to allow
    // file uploads

    // Make the form ready for image uploads



    Posted: 7 years ago #
  6. jimmyink


    Sorry for the late reply jevets, saw your message and forgot to get back to it. Let me know if this helps you Rohit.

    Posted: 7 years ago #
  7. jimmyink


    PS the foreach loop is not essential, I extracted this from a larger script. You can probably optimize this code by shedding some lines.

    Posted: 7 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.

  • Rating

    4 Votes
  • Status

    This is not a core suggestion