Sharding BLOGUPLOADDIR

Copy the body of wp-includes/ms-settings.php into sunrise.php, from line 25 to line 127. At the bottom, add your BLOGUPLOADDIR defines.

// from ms-settings.php
ms_subdomain_constants();

if ( !isset( $current_site ) || !isset( $current_blog ) ) {
    // [trimmed, but you need the whole if block]
}
// end of ms-settings.php copy

if ( $current_blog->blog_id < 10 ) {
    $bloggroup = 'global';
} else {
    $bloggroup = 'bloggroup' . floor( $current_blog->blog_id / 2000 + 1 ); // 1999->1, 2000->2
}

// from ms-default-constants.php: ms_upload_constants()
define( 'UPLOADBLOGSDIR', 'wp-content/blogs.dir' );
define( 'UPLOADS', UPLOADBLOGSDIR . "/{$bloggroup}/{$current_blog->blog_id}/files/" );
define( 'BLOGUPLOADDIR', WP_CONTENT_DIR . "/blogs.dir/{$bloggroup}/{$current_blog->blog_id}/files/" );

ms-settings.php will load sunrise.php. When execution returns to ms-settings.php, it will see that $current_site and $current_blog are set, and it will skip that huge if statement. Just remember to update your sunrise.php when you upgrade WordPress.

Extreme hacky solution that doesn’t require copypasta would involve one of the wp_start_object_cache() overrides (the only hookable functionality between discovering $current_blog and calling ms_upload_constants()), but let’s not go there.

Leave a Comment