I am looking for a way to tweak my xml parser code to insert a url from an external source into a new post, instead of first uploading it to the website's media library and than adding the attachment.

I have been looking for answers for the past week. But every time I google it, the search results come down to someone asking how to upload a attachment to a post, which I already have fixed.

But I did see a post where someone describes the function media_sideload_image(). But when I look at the code of that function it states, that the image is save temporarily, and I am not sure if that is a sustainable solution since I want the external url to always be used when displaying a post.

This is the code I am currently using:

        // Just some random image, this variable is normally dynamic
        $image_url        = "https://www.elegantthemes.com/blog/wp-content/uploads/2014/07/creating-a-wp-plugin.jpg";

        $upload_dir       = wp_upload_dir();
        $image_data       = file_get_contents($image_url);
        $unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name );
        $filename         = basename( $unique_file_name );
        if( wp_mkdir_p( $upload_dir['path'] ) ) {
            $file = $upload_dir['path'] . '/' . $filename;
        } else {
            $file = $upload_dir['basedir'] . '/' . $filename;
        file_put_contents( $file, $image_data );
        $wp_filetype = wp_check_filetype( $filename, null );
        $attachment = array(
            'post_mime_type' => $wp_filetype['type'],
            'post_title'     => $creditAuthorUrl,
            'post_content'   => '',
            'post_status'    => 'inherit'
        // This function inserts an attachment into the media library
        $attach_id = wp_insert_attachment( $attachment, $file, $post_id );
        // This function generates metadata for an image attachment. 
        // It also creates a thumbnail and other intermediate sizes of the image attachment based on the sizes defined
        $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
        // Update metadata for an attachment.
        wp_update_attachment_metadata( $attach_id, $attach_data );
        // Set a post thumbnail. So it links the attachment id to the corresponding post id
        set_post_thumbnail( $post_id, $attach_id );

And a small sidenote, the theme I use, uses wp_get_attachment_image_src() to get the image attachments. And it than uses the first item of that attachment as the image url to be displayed.

So basically my question is: how can I change my parser, so that the url in the image attachment is an external url instead of one I need to upload to the media library?

Thanks in advance!

  • Sorry, why can’t you just use an img tag in HTML? – Jacob Peattie Sep 6 '18 at 12:36
  • Yeah I looked into the code my theme uses to display the images, and it comes down to wp_get_attachment_src(), where it get's the url from that is displayed in every post. So basically I want to have that url be an external url instead one I first needed to upload to my media library. So that's why I am looking into my parsing code which uploads the image, because I want to only use the external url instead of uploading the image to my own media library – Vasco Sep 6 '18 at 12:41
  • Well as the name suggests, that function requires an attachment. You’ll need to change your theme to output a URL instead. – Jacob Peattie Sep 6 '18 at 12:43
  • So you mean by uploading the external image url as custom meta data, and than programming my theme to display the custom meta data url? – Vasco Sep 6 '18 at 12:46
  • Ooh yes it does output the url from the wp_get_attachment_src() by getting the first item of that return array – Vasco Sep 6 '18 at 12:48

Your Answer

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

Browse other questions tagged or ask your own question.