Skip to content

Instantly share code, notes, and snippets.

@raazon
Last active December 1, 2023 18:00
Show Gist options
  • Save raazon/a00c6d0c5b3c4b8346ed139d84d923ea to your computer and use it in GitHub Desktop.
Save raazon/a00c6d0c5b3c4b8346ed139d84d923ea to your computer and use it in GitHub Desktop.
How to add a featured image from a URL in WordPress
<?php
//----------------------------------------------------------------------
// Method 1: Upload image form extranal url via WP media_handle_sideload
// @return ID
//----------------------------------------------------------------------
function upload_image_from_external_url_media_handle_sideload($image_url = NULL){
if ( ! filter_var($image_url, FILTER_VALIDATE_URL) ) {
return;
}
// Need to require these files
if ( !function_exists('media_handle_upload') ) {
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}
// Add Featured Image to Post
$url = preg_replace('/\?.*/', '', $image_url); // removing query string from url & Define the image URL here
$tmp = download_url( $url );
if( is_wp_error( $tmp ) ){
// download failed, handle error
return;
}
$pathinfo = pathinfo($url);
/**
* now we can actually use media_handle_sideload
* we pass it the file array of the file to handle
* and the post id of the post to attach it to
* $post_id can be set to '0' to not attach it to any particular post
*/
$post_id = 0;
$desc = $pathinfo['filename'];
$file_array = array();
// Set variables for storage
// fix file filename for query strings
preg_match('/[^\?]+\.(jpg|jpe|jpeg|gif|png)/i', $url, $matches);
$file_array['name'] = basename($matches[0]);
$file_array['tmp_name'] = $tmp;
// If error storing temporarily, unlink
if ( is_wp_error( $tmp ) ) {
@unlink($file_array['tmp_name']);
$file_array['tmp_name'] = '';
}
// do the validation and storage stuff
$id = media_handle_sideload( $file_array, $post_id, $desc );
// If error storing permanently, unlink
if ( is_wp_error($id) ) {
@unlink($file_array['tmp_name']);
return $id;
}else{
return $id;
}
$src = wp_get_attachment_url( $id );
}
//----------------------------------------------------------------------
// set the featured image from extranal url
//----------------------------------------------------------------------
$attachment_id = upload_image_from_external_url_media_handle_sideload('https://s.w.org/about/images/logos/wordpress-logo-stacked-rgb.png');
if( $attachment_id ){
set_post_thumbnail( $post_id, $attachment_id ); // set featured image via $post_id and $attach_id
}
@dheeru46
Copy link

dheeru46 commented Jul 3, 2022

Your article is fabulous. but I have some issues, sir I don't want to upload image. I want to use external URL as feature image. basically I want use feed to publish article but I cant upload another website image and use my own URL .
I want use feature image from another website URL and when open post it redirect another website article.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment