Last active
December 1, 2023 18:00
-
-
Save raazon/a00c6d0c5b3c4b8346ed139d84d923ea to your computer and use it in GitHub Desktop.
How to add a featured image from a URL in WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//---------------------------------------------------------------------- | |
// Step 1: Create a Post Dynamically | |
//---------------------------------------------------------------------- | |
// Register Post Data | |
$post = array(); | |
$post['post_status'] = 'publish'; | |
$post['post_type'] = 'post'; // can be a CPT too | |
$post['post_title'] = 'My New Post'; | |
$post['post_content'] = 'My new post content'; | |
$post['post_author'] = 1; | |
// Create Post | |
$post_id = wp_insert_post( $post ); | |
// set featured image from extranal image | |
set_featured_image_from_external_url( 'http://s.wordpress.org/style/images/wp-header-logo.png', $post_id ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//---------------------------------------------------------------------- | |
// Method 2: set the featured image from extranal url | |
//---------------------------------------------------------------------- | |
function set_featured_image_from_external_url($url, $post_id){ | |
if ( ! filter_var($url, FILTER_VALIDATE_URL) || empty($post_id) ) { | |
return; | |
} | |
// Add Featured Image to Post | |
$image_url = preg_replace('/\?.*/', '', $url); // removing query string from url & Define the image URL here | |
$image_name = basename($image_url); | |
$upload_dir = wp_upload_dir(); // Set upload folder | |
$image_data = file_get_contents($url); // Get image data | |
$unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name ); // Generate unique name | |
$filename = basename( $unique_file_name ); // Create image file name | |
// Check folder permission and define file location | |
if( wp_mkdir_p( $upload_dir['path'] ) ) { | |
$file = $upload_dir['path'] . '/' . $filename; | |
} else { | |
$file = $upload_dir['basedir'] . '/' . $filename; | |
} | |
// Create the image file on the server | |
file_put_contents( $file, $image_data ); | |
// Check image file type | |
$wp_filetype = wp_check_filetype( $filename, null ); | |
// Set attachment data | |
$attachment = array( | |
'post_mime_type' => $wp_filetype['type'], | |
'post_title' => sanitize_file_name( $filename ), | |
'post_content' => '', | |
'post_status' => 'inherit' | |
); | |
// Create the attachment | |
$attach_id = wp_insert_attachment( $attachment, $file, $post_id ); | |
// Include image.php | |
require_once(ABSPATH . 'wp-admin/includes/image.php'); | |
// Define attachment metadata | |
$attach_data = wp_generate_attachment_metadata( $attach_id, $file ); | |
// Assign metadata to attachment | |
wp_update_attachment_metadata( $attach_id, $attach_data ); | |
// And finally assign featured image to post | |
set_post_thumbnail( $post_id, $attach_id ); | |
} | |
//---------------------------------------------------------------------- | |
// set the featured image from extranal url | |
//---------------------------------------------------------------------- | |
set_featured_image_from_external_url('https://s.w.org/about/images/logos/wordpress-logo-stacked-rgb.png', $post_id); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.