WordPress.org

Ready to get started?Download WordPress

Forums

inserting and scaling images using wp_insert_attachment (3 posts)

  1. greftek
    Member
    Posted 4 years ago #

    I'm currently trying to make a plug-in which downloads an image (based from an rss-feed) and insert this image into the media library.

    I know the media library can rescale images to a few predefined views (thumbnail, medium, large, etc). Based on behavior I know it does not do this 'on the fly'.

    Does wordpress automagically create these image views when I insert the attachments using wp_insert_attachment or is there something more to it? If so, how can I create these views?

  2. goldenapples
    Member
    Posted 3 years ago #

    I had the hardest time trying to figure out a similar issue to that. I was able to work around it by using the built in function media_handle_upload(). This wouldn't work in your case, as it only handles images uploaded in a $_FILES post. But you could take a look at the source of that function to see how it generates the intermediate image sizes.

    The function I ended up with, after tearing my hair out trying to make wp_insert_attachment work for me, was just real simply:

    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;
    	}

    where $file_handler refers to an object in the $_FILES superglobal.

    Hope this helps someone.

  3. goldenapples
    Member
    Posted 3 years ago #

    Correction: the $file_handler parameter above actually refers to a key in the $_FILES array (i.e. a file upload field from a submitted form), not the object itself.

Topic Closed

This topic has been closed to new replies.

About this Topic