1

So I want to change my uploads directory. I'm able to change it using:

add_filter( 'pre_option_upload_url_path', 'wpse_77960_upload_url' );
function wpse_77960_upload_url() {
  return 'http://subdomain.example.com/files';
}

or

update_option('upload_url_path', '/wp-content/uploads');

source

or something along the lines of:

register_setting( 'media', 'upload_url_path', 'esc_url' );

Is there a preferred way of doing this when it comes to overall speed? I tried using define( 'UPLOADS', 'http://absolute/path/to/dir' );, but it kept prepending the site-url to all the uploads.

1

You can use the 'upload_dir' filter

add_filter('upload_dir', 'set_upload_folder', 999);

function set_upload_folder( $upload_data ) { 
  // absolute dir path, must be writable by wordpress 
  $upload_data['basedir'] = trailingslashit(ABSPATH). '/files';
  $upload_data['baseurl'] = 'http://subdomain.wptest.com/files';
  $subdir = $upload_data['subdir'];
  $upload_data['path'] = $upload_data['basedir'] . $subdir;
  $upload_data['url'] = $upload_data['baseurl'] . $subdir;
  return wp_parse_args($upload_data, $upload_data);
}

This code will works no matter if year/month subfolders option is on or off.

Note that, of course, you need to configure your DNS to point the new url to the right folder.

| improve this answer | |
  • is there an advantage to this approach? – alme1304 Sep 26 '13 at 18:20
  • Upon the 'update_option' IMHO method yes, for the more granular control upon dir, url and subdir (e.g. you can add filter only for certain posts attached files, and so on). But there is no advantage in speed. – gmazzap Sep 26 '13 at 18:35

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.