3

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?

4
  • You need to show us some code or something because this isn't a question.
    – Wyck
    Sep 25, 2013 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, 2013 at 17:49
  • Use Auto Save Remote Image Plugin wordpress.org/plugins/auto-save-remote-images Oct 2, 2015 at 0:51
  • @mAAdhaTTah Did you create a plugin with this code?
    – David
    Oct 25, 2015 at 13:31

1 Answer 1

3

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

https://developer.wordpress.org/reference/functions/media_sideload_image/

4
  • 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, 2013 at 23:43
  • 1
    That is not correct, the function downloads the image.
    – Wyck
    Sep 26, 2013 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, 2013 at 0:27
  • That won't work unless your image has an explicit type - which is not the case for example when you use the LinkedIn API. So in those cases, you cannot use side load. You also cannot use that in the front end unless explicitly requiring require_once(ABSPATH . 'wp-admin/includes/media.php'); require_once(ABSPATH . 'wp-admin/includes/file.php'); require_once(ABSPATH . 'wp-admin/includes/image.php');
    – user75665
    Apr 3, 2023 at 3:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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