Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How can one get the blog ID as well as the database prefix of the main blog in a wordpress multisite installation? Is the main blog's ID always 1? Also how can the main blog's table prefix be acquired from within the mu-plugin's code?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

Use the constant BLOG_ID_CURRENT_SITE. And to get the database table prefix, use;

$main_blog_prefix = $wpdb->get_blog_prefix(BLOG_ID_CURRENT_SITE);
share|improve this answer
    
Thanks for the answer. Mind if I ask when BLOG_ID_CURRENT_SITE!=1? –  MikeSchinkel Dec 25 '10 at 0:22
    
@MikeSchinkel I'm not sure! It might be if you have multiple networks, or if you (somehow or other) switched the main blog with another? –  TheDeadMedic Dec 25 '10 at 1:59
    
Thanks. Andrew Nacin answered me on Twitter. twitter.com/nacin/status/18479360935723009 –  MikeSchinkel Dec 25 '10 at 4:41
add comment

This is how we do it to be 100% as we have multiple main sites in the network installation:

function name_ly_get_main_blog_id () {
  global $current_site;
  global $wpdb;

  return $wpdb->get_var ( $wpdb->prepare ( "SELECT `blog_id` FROM `$wpdb->blogs` WHERE `domain` = '%s' AND `path` = '%s' ORDER BY `blog_id` ASC LIMIT 1", $current_site->domain, $current_site->path ) );
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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