Currently I'm trying to take screenshots of websites. That works great by using mshots and media_handle_sideload.

However these screenshots need to be associated with my post. I've made a function called

upload_image_from_url($url, $post_id)

and passed the $post_id into the function

$id = media_handle_sideload( $file_array, $post_id);

Now the returned value for $id is incremented by 1 from $post_id, e.g. $post_id is 200 and $id is 201. Is it not supposed to be the same id?


Okay I hate it when I just figure it out after I post the question.

But it seems each post can have a number of attachments. Which means each attachment has an ID associated with it. Which was confusing because it shared the same ID style and type as the post.

The following code is able to get the URL of the associated uploaded image

$media = get_attached_media( 'image', $post_id ); 
foreach($media as $image) {
  $url_img = wp_get_attachment_image_src($image->ID,'full');
  echo $url_img[0];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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