[wp-hackers] Multisite options

David Anderson david at wordshell.net
Mon Jan 20 10:02:01 UTC 2014


Hi,

I am working with code that wants to get the WordPress site's base table 
prefix on a WPMU install. i.e. The original $table_prefix - not its 
*current* value (which can be changed due to site switching).

This is available as $wpdb->base_prefix. However, this is only public by 
default. The class itself does not explicitly mark it as public or 
private. So, I reasoned that probable $wpdb->get_blog_prefix() is the 
"proper" way to read it.

The code of that function is as follows:

     function get_blog_prefix( $blog_id = null ) {

         if ( is_multisite() ) {

             if ( null === $blog_id )

                 $blog_id = $this->blogid;

             $blog_id = (int) $blog_id;

             if ( defined( 'MULTISITE' ) && ( 0 == $blog_id || 1 == $blog_id ) )

                 return $this->base_prefix;

             else

                 return $this->base_prefix . $blog_id . '_';

         } else {

             return $this->base_prefix;

         }

     }

It thus looks like $wpdb->get_blog_prefix(0) is the way to get the value 
I want. However, I am confused about the purpose of the block that 
begins with if (defined( 'MULTISITE' )).

What is the specific purpose of the defined('MULTISITE') check? One 
might guess that MULTISITE will always be defined if is_multisite() 
passed. However, a look at is_multisite() shows that this is not 
necessarily so:

function is_multisite() {

     if ( defined( 'MULTISITE' ) )

         return MULTISITE;

     if ( defined( 'SUBDOMAIN_INSTALL' ) || defined( 'VHOST' ) || defined( 'SUNRISE' ) )

         return true;

     return false;

}

So, is_multisite() can return true even if MULTISITE is not true; there 
are the additional situations where any of the 3 constants 
SUBDOMAIN_INSTALL, VHOST or SUNRISE are defined (regardless of their value).

In that situation, $wpdb->get_blog_prefix() will actually return the 
value of: $this->base_prefix . '0_'

That doesn't seem like it can be correct. On the other hand, the code 
seems to be careful to bring about this result. It seems to be 
deliberately engineered with the possibility that is_multisite() is true 
but MULTISITE is not true.

Can someone give me a clue with what's going on here? I'm asking because 
I've just come across a user for whom $wpdb->get_blog_prefix() does 
actually return $this->base_prefix . '0_' - and he has no tables at all 
with that prefix. It seems like the wrong result. I've not yet got the 
information on the value of all those constants from him, but he says 
it's a WPMU install. I'm looking into whether his install is set up 
wrongly, but my question to wp-hackers is as to whether the 
$wpdb->get_blog_prefix() is functioning as intended, or whether this is 
a subtle bug, or whether I'm simply getting the base prefix in the wrong 
way and should be using a different strategy.

Many thanks,
David

-- 
UpdraftPlus - best WordPress backups - http://updraftplus.com
WordShell - WordPress fast from the CLI - http://wordshell.net



More information about the wp-hackers mailing list