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?
Leave a Reply
You must be logged in to post a comment.
Use the constant
BLOG_ID_CURRENT_SITE
. And to get the database table prefix, use;This is how we do it to be 100% as we have multiple main sites in the network installation:
You can use the below to get the table prefix.
There’s a function to get main site id for a given network (defaults to the active network)
If you only need db prefix,
$wpdb->get_blog_prefix(get_main_site_id())
would suffice, but otherwise you can switch whole code blocks into other blog’s context seamlessly withswitch_to_blog()
andrestore_current_blog()
functionsBut be wary of recursive calls to
switch_to_blog()
, because then context becomes hard to track and debug