1

I have moved my uploads directory to another server. For example, a url that was pointing to:

http://example.com/wp-content/uploads/2015/05/image.jpg

should point to here in the new situation:

http://otherexample.com/2015/05/image.jpg

I know I could do this by a rewrite rule or by a database search and replace, but for various reasons this is not what we want.

Now I know that it is possible to change the urls of posts etc. by using the filter hooks post_link, page_link and post_type_link. I was wondering, is there also a hook that can be used for changing image urls when they are requested from the system?

I primarily focus on the urls for featured images and such, as images in the post content are plain text in the database.

Regards, Joost.

1

As you want to change (use different) URL for featured image only, you can use pre_option_upload_url_path hook.

// Setting the uploads directory URL
function wpse_change_featured_img_url() {
  return 'http://www.example.com/media/uploads';
}
add_filter( 'pre_option_upload_url_path', 'wpse_change_featured_img_url' );

This hook will not change URLs permanently but it will set uploads directory to some different URL temporally.

This will also not change URL of post images, you will have to change them manually or with SQL query.

  • Thanks, this seems to work. However, has this any repercussions for the actual upload? Uploading using wordpress to that url would obviously not work... – jberculo May 28 '15 at 13:36
  • No you cannot upload remotely with this function. You will have to find any other way to do that. – Robert hue May 28 '15 at 14:04
  • I am not trying, so that's no problem :-) That's already taken care of... – jberculo May 29 '15 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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