Change ouput url of WordPress [Gallery] Shortcode Change ouput url of WordPress [Gallery] Shortcode wordpress wordpress

Change ouput url of WordPress [Gallery] Shortcode


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


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.


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';