media_sideload_image generates blank?

I’m creating a custom post form that is supposed to take an external image and uploads it to the media library and attach it to that post. This should be possible by using media_sideload_image, but I’m getting a blank page instead of working correctly, no error either. My code:

// Add the content of the form to $post as an array
        $new_post = array(
            'post_title'    => $title,
            'post_content'  => $description,
            'tags_input'    => $tags,
            'post_type'     => 'link_submit',
            'post_status'   => 'publish'
        //save the new post and return its ID
        if ($stop == false){
            $pid = wp_insert_post($new_post);
                    // attach image
            $upload = media_sideload_image($link, $pid, "test");
            if ( is_wp_error( $upload ) ){
            die( 'Nope' );

            $link = get_permalink( $pid );
            wp_redirect( $link."?posted" );



edit: new code:

if (is_page('Upp')){
    require_once(ABSPATH . 'wp-admin/includes/file.php');
    require_once(ABSPATH . 'wp-admin/includes/media.php');
    $upload = media_sideload_image("", "202", "my description");
    die ( $upload );

Solutions Collecting From Web of "media_sideload_image generates blank?"

Firstly, reverse the order in your conditional. You should use if ( false == $stop ) { ... It’s called a “Yoda conditional” and is on the list of best practices for WP code.

Secondly, media_sideload_image won’t return a WP error object if things fail. If things fail, it will return the ID of the post passed in ($pid in your case). If successful it will return the HTML to render the image just inserted.

I would recommend just dumping the contents of $upload to see what’s in there and see if things are successful. So the if(is_wp_error()) code … replace it with wp_die( $upload ) and see what comes out. If it’s an id, then something’s wrong on your system and you need to investigate why your server can’t download images. If it’s HTML markup for an image, then the image is being added and you’re ready to go.

For reference, take a look at the actual media_sideload_image() function in /wp-admin/includes/media.php around line 569.

Edit: 6/20/11

I took a deeper look at the code, and started walking through breakpoints I set in the WordPress code, which is where I finally found the problem.

While processing the image, media_sideload_image()will eventually make a call to media_handle_image(), which is in the same file. No problems here. But this other function makes a call to wp_read_image_metadata(), which is a function defined in another file that we haven’t yet included!

Unfortunately for us (and it obviously hasn’t been discovered yet because this code goes against WP best practices), there’s a nasty @ symbol before that function call. The @ symbol in PHP will silence any errors thrown by the parser, so PHP sees a “function not defined” error and stops … giving us a blank page and leaving us scratching our heads.

So adding the following line to the list of require_once()s will actually load and insert the image:

require_once(ABSPATH . 'wp-admin/includes/image.php');

The only other thing giving me a headache was that you code was for post_type=link_post … I didn’t have that post type defined, so I was running in circles trying to figure out why wp_insert_post() wasn’t working … it was, and I now have 100 “Test Post”s in my DB 🙂