5

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

4
  • 1
    Does it mean you have moved you upload folder to Amazon or only a few images? Commented Oct 28, 2013 at 7:43
  • Could you use the post_thumbnail_html filter? Commented Oct 28, 2013 at 7:55
  • I have uploaded all images to s3 . I need the image src to fetch from the amazon s3 url instead of old path. <a href="<?php the_permalink(); //show permalink?>"> <?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it. the_post_thumbnail(); }</a> How shall I modify the above code to change the image source url?Can it be done via db scripts?
    – user40991
    Commented Oct 28, 2013 at 9:57
  • Check out WP Offload S3.
    – jgraup
    Commented Nov 29, 2015 at 16:57

2 Answers 2

2

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:

0

Then you have to point the uploads folder to the same location. You can do it by making the below changes in the wp-config.php file

Open up your wp-config.php file, located at the root of your WordPress installation, and add the following code:

define('UPLOADS', 'http://amazon_url/uploads'); //Replace the url here with folder in Amazon

The codex specifies that it should be added before the line that says require_once(ABSPATH.’wp-settings.php’);.

Make sure the uploads folder is writable.

3
  • Thanks.The above helps to point the existing images to s3 path.But while trying to upload a new image,it gives error "Unable to create directory /wp-content/uploads/2009/mydir/summer/ Is its parent directory writable by the server?".If I comment the code added in config file, it is getting uploaded to s3. I am using wordpress plugin "Amazon S3 and cloudfront".
    – user40991
    Commented Oct 28, 2013 at 18:56
  • 1
    Hi,In addition the doc saysThis path can not be absolute. It is always relative to ABSPATH codex.wordpress.org/Editing_wp-config.php#Moving_uploads_folder. Therefore,can we give the http path as above?
    – user40991
    Commented Oct 28, 2013 at 19:23
  • yes you can, esp when you have the uploads folder in a separate domain. please make sure the folder is writable to make this work and upload further Commented Oct 29, 2013 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.