How to Add a Featured Image from a URL in WordPress

By WP Saviour •  Updated: 03/04/21 •  3 min read

Usually it is best to dynamically add a featured image to a put up or a personalized put up kind from one different server than the one your WordPress arrange is working. And in addition you’re coping with a regular state of affairs: learn the way to do it?

In any case on this case, it is best to seize the featured image from the second server, receive it to your particular person server, inside the add folder, and assign it to the correct put up. In the 1st step we’ll create a model new put up dynamically, after which we’ll care for the featured image.

Step 1: Create a Put up Dynamically

To dynamically create a put up, it is best to use the wp_insert_post() function. You can place the code beneath in an “IF” assertion, in every other case each time an internet web page is loaded it ought to create a model new put up. Not that useful.

$put up = array();
$put up['post_status'] = 'publish';
$put up['post_type'] = 'put up';$put up['post_title'] = 'My New Put up';
$put up['post_content'] = 'My new put up content material materials';
$put up['post_author'] = 1;
$post_id = wp_insert_post( $put up );

Working this code will merely create a model new put up. Now it’s time in order so as to add the featured image.

In order so as to add the featured image from a URL, we now need to make use of some WordPress options:

And now proper right here is the code. I commented each movement with the intention to see exactly what’s going down when this script is working.

$image_url = '';$image_name = 'wp-header-logo.png';
$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' => sanitize_file_name( $filename ), 'post_content' => '', 'post_status' => 'inherit'
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
require_once(ABSPATH . 'wp-admin/consists of/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );

What’s fascinating on this code is that you’d have the ability to place it in a loop. As an illustration to import posts from a CSV file, or an XML file. That’s really extremely efficient and truly useful, nevertheless don’t overlook  one issue: in no way ever use this script in your options.php file with out inserting a conditional tag sooner than in every other case you’ll get a lot of of current posts created in a matter of minutes!

gp-9334124 as-9010288

WP Saviour

I am a WordPress specialist. My mission is to help you create beautiful websites with ease!