2

This isn't the usual "How do I change the media upload folder?" question. Our entire site is run through a CDN. The CDN drops POST requests with a 5-6MB file so the WordPress uploads fail. I can work around this I think by changing where WordPress sends file upload requests.

If the normal site URL is www.example.com, I want uploads to be sent to just example.com which is our origin server and will have no problem handling larger file uploads. Any ideas?

3

It was actually easier than I thought.

function route_uploads_past_cdn( $url, $path ) {
    $upload_paths = array( 'async-upload.php', 'media-new.php' );
    if( !in_array( $path, $upload_paths ) ) {
        return $url;
    }

    return str_replace('www.', '', $url);
}
add_filter( 'admin_url', 'pew_route_uploads_past_cdn', 10, 2 );

The URL for uploading media would normally be http://www.example.com/wp-admin/media-new.php would now be http://example.com/wp-admin/media-new.php since the URLs are passed through admin_url() which you can filter.

  • Glad you solved it. – jimihenrik Mar 24 '15 at 21:48
0

I'm not quite sure what do you mean.

You can change from where the WP loads the files if that's what you mean? add_filter( 'pre_option_upload_url_path', function() { return 'http://yoursite.com/wp-content/uploads'; } );

That's what I use to develop on a local server with all the "real stuff/uploads" being in the live servers anyway.

edit: so no, this won't change where your uploads end from the admin, this just changes where the WP loads the media files from. To change the default folder where media uploads will end I think you need a custom upload function.

edit2: quote from here

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

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.