Unmasking file locations in a WordPress network

By on Apr 3rd

The Offbeat Empire uses a WordPress network to manage all of our sites in order to simplify site-wide updates, like keeping plugins and themes up to date. Because of our customized web server setup, it is better for our nginx server to bypass WordPress's ms-files.php and just serve uploaded files from the actual location. Users don't notice the difference, and having the blog_id visible in the HTML source is not a major concern as all of our sites are sister sites.

For WordPress 3.1:

function ucc_fix_upload_dir( $uploads ) {
  // Set these options in Network Admin > Sites > Edit > Settings > Upload Path and Upload Url Path.
  $upload_path = get_option( 'upload_path' );       //  Example: /home/username/public_html/wp-content/blogs.dir/2/files
  $upload_url_path = get_option( 'upload_url_path' ); //  Example: http://sub.domain.com/wp-content/blogs.dir/2/files
  
  $uploads[ 'baseurl' ] = $upload_url_path;
  $uploads[ 'url' ] = $upload_url_path . $uploads[ 'subdir' ];
  $uploads[ 'basedir' ] = $upload_path;
  $uploads[ 'path' ] = $upload_path . $uploads[ 'subdir' ];

  return $uploads;
}
add_filter( 'upload_dir', 'ucc_fix_upload_dir' );

For WordPress 3.0:

function ucc_fix_upload_dir( $uploads ) {
  // Set these options in Super Admin > Sites > Edit > Upload Path and Upload Url Path.
  $upload_path = get_option( 'upload_path' );       //  Example: /home/username/public_html/wp-content/blogs.dir/2/files
  $fileupload_url = get_option( 'fileupload_url' ); //  Example: http://sub.domain.com/wp-content/blogs.dir/2/files

  $uploads[ 'baseurl' ] = $fileupload_url;
  $uploads[ 'url' ] = $fileupload_url . $uploads[ 'subdir' ];
  $uploads[ 'basedir' ] = $upload_path;
  $uploads[ 'path' ] = $upload_path . $uploads[ 'subdir' ];

  return $uploads;
}
add_filter( 'upload_dir', 'ucc_fix_upload_dir' );

Don't forget to comment out the call to ms-files.php in the .htaccess file to enable this.

Code contained in this entry is licensed under GNU General Public License, version 2.

References

#13483 ( fileupload_url / upload_url_path )

Read more posts about: , ,