I have uploaded my images to Amazon S3. There are some featured images. In order to modify their path, I run the update scripts to change the path of wp_posts.guid
. But the image still points to the old path.
How can I change the featured image url so as to point to the Amazon S3 path?
Thanks
You can hook into the output and modify the URL there.
add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 5 );
function my_post_image_html( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
$upload_dir = wp_upload_dir();
$base_url = $upload_dir['baseurl'];
// Change the default upload directory to AWS Bucket link
$AWSBucket="http://s3.amazonaws.com/bucket";
$html = str_replace($base_url, $AWSBucket, $html);
return $html;
}
Output the image
echo get_the_post_thumbnail ();
Reference:
- https://developer.wordpress.org/reference/hooks/post_thumbnail_html/
- https://codex.wordpress.org/Function_Reference/wp_upload_dir
- https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/