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:

Read More
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?

Related posts

Leave a Reply

2 comments

  1. 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.

  2. Define it in wp-config.php. The problem is you won’t have access to the $wpdb->blogid but try to create a special internal encoding for each blog (by subdomain or domain/first-slice-of-path). In worst case, you can do a quick mysql connection and grab the blog ID yourself. Once you find it in the DB, store it in an XML or something.

    Plugins kick in way after the constant is defined so, unless you’re willing to do a tiny hack to the core :)… your only chance is the use of wp-config.php plus some **custom blog_id**s you’d have to manually (by code) generate and manage.

    It’s doable, it just requires a bit of extra work, custom tailored for your environment…