4

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

4
+50

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.

1
  • didnt try yet but accepting as answer. probably there is no better solution.. thank you :) Commented Nov 2, 2011 at 11:32
-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...

4
  • create a special internal encoding for each blog? we talking about 30.000+ blog here.. Commented Oct 24, 2011 at 1:42
  • Use the database. That's why I said it's custom. It depends on your structure, number of blogs... etc. You'd need to test and see if there are performance penalties... It can be done, it's just that it's not a general solution. Do you use Subdomains or Paths? Commented Oct 24, 2011 at 1:46
  • 2
    sorry but sunrise.php file makes more sense then wp-config. btw you can write code and we can check it if u want, still i dont think you can get blog_id properly from wp-config Commented Oct 24, 2011 at 1:57
  • I think sunrise.php is loaded before blogid is available. Just hack the ms_upload_constants() in /wp-includes/ms-default-constants.php. You have blogid there and just pay attention to carry over the change on updates. Commented Oct 24, 2011 at 2:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.