Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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);
            update_post_meta($pid,'Link',$link,true);
            update_post_meta($pid,'Type',$type,true);
                    // attach image
            set_time_limit(300);
            $upload = media_sideload_image($link, $pid, "test");
            if ( is_wp_error( $upload ) ){
            die( 'Nope' );
            }

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

Ideas?

Thanks, Dennis

edit: new code:

if (is_page('Upp')){
    require_once(ABSPATH . 'wp-admin/includes/file.php');
    require_once(ABSPATH . 'wp-admin/includes/media.php');
    set_time_limit(300);
    $upload = media_sideload_image("http://29.media.tumblr.com/tumblr_lmp1tbiYwh1qzlfumo1_500.jpg", "202", "my description");
    die ( $upload );
}
share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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 :-)

share|improve this answer
    
Hi EAMann, first of all, thanks for answering my question. I've been trying to follow your advice by creating a simplified version in the header.php, however, $upload still doesn't return anything. My example is here. I've also edited the post with the new code. As I'd like to rule out my server of being the culprit, can you check if this code works for you? Many thanks –  FLX Jun 13 '11 at 16:14
    
I'll need to take a closer look at things tonight, but something fishy is definitely going on. Your code half works. The image is downloaded to my server and stored in the /wp-content/uploads folder as I expect, but it's not associated with a post and the site just gives me a blank page instead of moving forward. As I said, I'll take a closer look as soon as I can, but for now maybe taking a look at Press This (the only part of WP that uses media_sideload_image()) might help ... –  EAMann Jun 13 '11 at 17:31
    
Hmm, it appears you're right, I also have the images on server but not registered in the Media Library. I tried the code from press-this.php but got the same behavior. I'll keep on trying, please let me know if you've found out why this happens. Thanks for all the effort! –  FLX Jun 13 '11 at 18:53
    
Hi EAMann, I still haven't been able to use media_sideload_image, did you make any progress? Thanks :) –  FLX Jun 15 '11 at 10:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.