How can I use an image from an external source without uploading it to the media library
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!
Topic parse media-library post-thumbnails attachments images Wordpress
Category Web