WordPress.org

Ready to get started?Download WordPress

Ideas

'wp_handle_upload' Should Create the Resized Versions of Images

  1. jimmyink
    Member

    12345

    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: 3 years ago #
  2. jimmyink
    Member

    12345

    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: 3 years ago #
  3. jevets
    Member

    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: 3 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
    Rohit

    Posted: 3 years ago #
  5. jimmyink
    Member

    12345


    <?php
    //
    // 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.
    unset($_FILES[$field]);
    }
    }
    }

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

    /*
    jQuery(document).ready(function(){
    // Make the form ready for image uploads
    jQuery('form#post').attr('enctype','multipart/form-data');
    jQuery('form#post').attr('encoding','multipart/form-data');
    }

    */

    ?>

    Posted: 3 years ago #
  6. jimmyink
    Member

    12345

    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: 3 years ago #
  7. jimmyink
    Member

    12345

    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: 3 years ago #

RSS feed for this topic

Reply

You must log in to post.

  • Rating

    12345
    4 Votes
  • Status

    This idea is under consideration