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 );
}
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 theif(is_wp_error())
code … replace it withwp_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 tomedia_handle_image()
, which is in the same file. No problems here. But this other function makes a call towp_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: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 whywp_insert_post()
wasn’t working … it was, and I now have 100 “Test Post”s in my DB 🙂