Support » Plugins and Hacks » inserting and scaling images using wp_insert_attachment

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

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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.

    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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘inserting and scaling images using wp_insert_attachment’ is closed to new replies.