Sharding BLOGUPLOADDIR

I am trying to sharding BLOGUPLOADDIR but couldnt success atm. This is default one:

define( 'BLOGUPLOADDIR', WP_CONTENT_DIR . "/blogs.dir/{$wpdb->blogid}/files/" );

I am trying to set it:

if($wpdb->blogid<10){ 
    $bloggroup = 'global';
}else{
    $bloggroup = 'bloggroup'.floor($wpdb->blogid/2000+1); // 1999->1, 2000->2
}
define( 'BLOGUPLOADDIR', WP_CONTENT_DIR . "/blogs.dir/{$bloggroup}/{$wpdb->blogid}/files/" );

So every 2000 blog, bloggroup will change.. I tried upload_dir filter but i need to define before it. I tried sunrise.php but $wpdb->blogid is not defined in there it seems. Whats propery way of this?

2 Answers
2

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