WordPress Posts: how to set featured image from remote URL programmatically

/**
 * Sets featured image by URL.
 *
 * @param $post_id
 * @param $url
 * @param $description
 *
 *
 * @return bool|int
 */
function wpm_set_post_featured_image_by_url($post_id, $url, $description){
	require_once(ABSPATH . 'wp-admin/includes/media.php');
	require_once(ABSPATH . 'wp-admin/includes/file.php');
	require_once(ABSPATH . 'wp-admin/includes/image.php');

	$image = media_sideload_image( $url, $post_id, $description,'id' );

	return set_post_thumbnail( $post_id, $image );
}

wpm_set_post_featured_image_by_url($post_id, 'https://via.placeholder.com/600x480?ext=.jpg', "Description for that image");

Get in touch!

Send us a message, we are
happy to provide assistance or a project quote.