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 using Embedly to make it easy for a client to add news to their website - basically, they just input a URL, and Embedly grabs the link's information, and I add it all to post meta for displaying on the front-end. So far, so good.

Currently, I'm using the thumbnail as it's served from the URL's website, but what I'd like to do is download the image, add it to the media library, and set it as the featured image. I'm given the image's URL, and I think I have to use wp_remote_get to download the image, but I have no idea what the rest of the steps are.

Edit: Here's the relevant bits:

function get_fr_news_embedly( $post_id ) {
$post = get_post( $post_id );

if ( $post->post_type == 'fr_news' ) {

    $embedly_key = '[key goes here]';

    $api = new Embedly\Embedly(array( 'key' => $embedly_key, 'user_agent' => 'Mozilla/5.0 (compatible; mytestapp/1.0)' ));
    $fr_news_url = $post->fr_news_url;

    $fr_news_objs = $api->oembed( $fr_news_url );
    update_post_meta( $post_id, 'fr_news_objs', $fr_news_objs );
    $fr_news_objs = get_post_meta( $post_id, 'fr_news_objs' );

    foreach ($fr_news_objs as $k => $oembed) {
        $oembed = (array) $oembed;

        if (array_key_exists('thumbnail_url', $oembed)) {
            $fr_news_thumbnail_url = $oembed['thumbnail_url'];
            update_post_meta( $post_id, 'fr_news_thumbnail_url', $fr_news_thumbnail_url );
            $image = wp_remote_get( $fr_news_thumbnail_url );
                            // wat do?
        }

The question comes down to this: if you have a url for an image, how do you download that image and add it to the media library?

share|improve this question
    
You need to show us some code or something because this isn't a question. –  Wyck Sep 25 '13 at 17:40
    
I added the relevant parts, but part of the problem is I'm not really sure where to look. The question comes down to this: if you have a url for an image, how do you download that image and add it to the media library? –  mAAdhaTTah Sep 25 '13 at 17:49

1 Answer 1

up vote 0 down vote accepted

I don't have a code example to test atm but to download images via an URL, in your case after $image = wp_remote_get( $fr_news_thumbnail_url ); you can use the function:

media_sideload_image($file, $post_id, $desc);

http://codex.wordpress.org/Function_Reference/media_sideload_image

share|improve this answer
    
That outputs a "populated HTML img tag" - it doesn't download the image or anything, so that's not going to work. –  mAAdhaTTah Sep 25 '13 at 23:43
    
That is not correct, the function downloads the image. –  Wyck Sep 26 '13 at 16:13
    
My mistake, you are correct. I ended up using a modified version of that function so I could also make it the featured image. I'm going to edit your answer with my final solution and mark this answered. Thanks for your help! –  mAAdhaTTah Sep 27 '13 at 0:27

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.