The essence of the question is, I'm looking for a solution like this:

$blog_upload_dir_info = wp_upload_dir( $date, $blog_ID );
$blog_upload_url = $blog_upload_dir_info[ 'baseurl' ];

Where $blog_ID is different of the current blog ID. The most »WP-obvious« solution fails:

switch_to_blog( $blog_ID );
$blog_upload_dir_info = wp_upload_dir();

as wp_upload_dir() relies on the constant WP_CONTENT_URL which is dynamically set the URL of the current blog unless the option upload_url_path is set.

Of course, I could set this option, but this would couple my code to concrete system settings which includes much »WTF?«-potential.

So I decided to add this option virtually:

 * Apply a value to the option blog_upload_url 
 * if there's not already one 
 * @wp-hook option_upload_url_path
 * @param string $upload_url
 * @return string
function get_real_blog_upload_url( $upload_url ) {

    if ( '' !== trim( $upload_url ) )
        return $upload_url;

    $upload_path = trim( get_option( 'upload_path' ) );
    $siteurl = get_option( 'siteurl' );
    $wp_content_dir = $siteurl . '/wp-content';

    if ( empty( $upload_path ) || 'wp-content/uploads' == $upload_path ) {
        $dir = $wp_content_dir . '/uploads';
    } elseif ( 0 !== strpos( $upload_path, ABSPATH ) ) {
        // $dir is absolute, $upload_path is (maybe) relative to ABSPATH
        $dir = path_join( ABSPATH, $upload_path );
    } else {
        $dir = $upload_path;

    if ( empty( $upload_path ) || ( 'wp-content/uploads' == $upload_path ) || ( $upload_path == $dir ) )
        $upload_path = $wp_content_dir . '/uploads';
        $upload_path = trailingslashit( $siteurl ) . $upload_path;

    return $upload_path;

which is in fact a partly fork of wp_upload_dir() and as such relies on constants which isn't a good practice at all. Moreover a fork is always coupled to the original implementation and if the original changes, one also have to fix the fork.

So as this solution is far away from being perfect, I wonder if there's a better way to get upload URLs by blog IDs.

  • 1
    There's another workaround for this problem in the Plugin Multilingual Press: github.com/inpsyde/multilingual-press/blob/… – David May 1 '15 at 12:30
  • 1
    I think it is helpful that you add the source as answer for other readers. The link is not stable and outside the WPSE Answer. – bueltge May 6 '15 at 8:20
  • 1
    @bueltge Basically I agree. But my comment relying to this workaround was a bit overhasty. The solution is an approach for this problem, but it has some limitations: github.com/inpsyde/multilingual-press/issues/133 When we found a solution, I of course will answer it here too. – David Jul 10 '15 at 7:30

Why not just use get_option('upload_path') after your switch_to_blog( $blog_ID );? Does that do it?

| improve this answer | |
  • (Build the full URL by using get_blog_details codex.wordpress.org/WPMU_Functions/get_blog_details) – Megan Feb 25 '16 at 12:46
  • Look into wp_upload_dir(): The option upload_path is not a default option and might be empty. Further it could be omitted by the option upload_url_path. However, a proper solution for this problem would be to fork wp_upload_dir() completely to cover all this possible settings and edge cases. – David Feb 25 '16 at 16:48

For completeness' sake, this solution seems to work until the issue is fixed in core:

add_action('switch_blog', function($new_blog, $prev_blog_id) {
    update_option( 'upload_url_path',  get_option('siteurl') . '/wp-content/uploads');
}, 10, 2);
| improve this answer | |
  • 1
    Yes, that should work in standard WordPress installs. However, I wouldn't want to use this due to the DB write action. This will make switch_to_blog() even slower. – David Oct 14 '16 at 14:16
  • This is also assuming that your content url is actually wp-content/uploads, which may not be the case. – rfair404 Apr 18 '17 at 16:50

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.