0

I am using is_main_site() on WordPress Multisite and when I set a break point and view with the debugger it returns true on the primary site and also on a child site I created. Looking at the code I can't see how it would ever return something other than true for a multisite install if the $site_id and $current_site->blog_id are supposed to be two different ways of accessing the same thing (the ID of the site you are currently on. Am I going crazy? Why does this not seem to work? I am running WordPress 4.0.1.

function is_main_site( $site_id = null ) {
    // This is the current network's information; 'site' is old terminology.
    global $current_site;

    if ( ! is_multisite() )
        return true;

    if ( ! $site_id )
        $site_id = get_current_blog_id();

    return (int) $site_id === (int) $current_site->blog_id;
}
  • Have you turned debugging and PHP error messages on? And in what context (where) have you used it? – kaiser Dec 12 '14 at 3:58
  • I stated in the post I was viewing it with the debugger. I am calling it from a MU plugin. It always returns true. – Adam Dec 12 '14 at 4:09
  • What does "with the debugger" mean? Have you turned PHP errors on? Are you running it with WP_DEBUG defined as true? And on which hook are you running it? plugins_loaded for e.g.? – kaiser Dec 12 '14 at 4:11
  • I am using WP_DEBUG=true which usually shows any errors but there is no error as I am getting true back. I am questioning whether the is_main_site() method in core is correct. I am not calling it inside of a hook but just straight away as the first thing in a MU plugin. – Adam Dec 12 '14 at 4:26
2

I am not calling it inside of a hook but just straight away as the first thing in a MU plugin.

At that point all the info you are requesting won't be available. That's running before any hook and before WP is fully loaded.

Use a hook like wp_loaded, admin_init, after_setup_theme ... whatever serves your purpose best.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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