5
votes

I recently installed ta plugin that now uploads my images from the media library to s3.

I have also FTP's the entire uploads folder to s3 which encompasses about 4000 images.

I have used throughout my site the wordpress gallery shortcode however somewhere and somehow it outputs the siteurl.

How do I change this so that I can override the url to be the one from my S3 bucket?

I will admit I have no idea what I am doing here or where to start and I really will appreciate your help :)

3
Can you update with which plugin have you installed? I think there are many for accomplishing this task. Some plugins do not support files when directly being uploaded to S3.Christos Lytras

3 Answers

2
votes

You can filter images src attribute output and replace old url with new url as follow. copy below code to your theme functions.php and replace www.oldurl.com and www.newurl.com with your own urls.

add_filter('wp_get_attachment_image_src', function ($image) {
    if(is_array($image)){
        $image[0] = str_replace('www.oldurl.com', 'www.newurl.com', $image[0]);
    }

    return $image;
}, 999);
2
votes

Looking into the wp_get_upload_dir(), that's a wrapper for wp_upload_dir(), that's again wrapper for _wp_upload_dir(), we see that the upload url can be modified through the upload_url_path option.

Since you're moving all of your uploads to S3, you could try to add your S3 bucket base url into the upload_url_path option.

You should test this first on your dev install, just to see how it works with your current setup.

1
votes

You might want to make a search & replace via the database.

You can see here how to make a SQL Query to change the Image Path in the posts: 13 Useful WordPress Queries

UPDATE wp_posts SET post_content = REPLACE (post_content, 'src="http://www.oldsiteurl.com', 'src="http://yourcdn.newsiteurl.com');
UPDATE wp_posts SET  guid = REPLACE (guid, 'http://www.oldsiteurl.com', 'http://yourcdn.newsiteurl.com') WHERE post_type = 'attachment';