Adding a Featured Image in WordPress from URL

Adding a Featured Image to a Post or Custom Post Type from Another Server

If you’re running a WordPress website and need to add a featured image to a post or custom post type from another server, you may be wondering how to do it. Fortunately, it’s possible to grab the featured image from the second server, download it to your own server’s upload folder, and assign it to the correct post. In this article, we’ll walk you through the steps to dynamically create a post and add a featured image from a URL.

Step 1: Create a Post Dynamically

To dynamically create a post, you can use the wp_insert_post() function. However, it’s important to place the code in an “IF” statement so that a new post isn’t created each time a page is loaded. Here’s an example of the code:

“`

// 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 );

“`

Running this code will create a new post dynamically. Now, let’s move on to adding the featured image.

Step 2: Add the Featured Image

To add the featured image from a URL, we’ll need to use some WordPress functions. Here’s the code:

“`

// Add Featured Image to Post

$image_url = ‘http://s.wordpress.org/style/images/wp-header-logo.png’; // Define the image URL here

$image_name = ‘wp-header-logo.png’;

$upload_dir = wp_upload_dir(); // Set upload folder

$image_data = file_get_contents($image_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 );

// Assign featured image to post

set_post_thumbnail( $post_id, $attach_id );

“`

This code will add the featured image to the post dynamically. You can also place this code in a loop to import posts from a CSV or XML file.

However, it’s important to note that you should never use this script in your functions.php file without placing a conditional tag before it, or you’ll end up with hundreds of new posts created in just a few minutes.

We hope you found this snippet useful. If you have any questions or comments, please leave them below!

Stay in Touch

spot_img

Related Articles