Find out if WordPress multisite uses subdirectory or subdomain for new blogs

Is there a function to check if a WordPress multisite install uses the subdirectory or subdomain option when new blogs get created?

I’m using the function wpmu_create_blog to create new blogs.
The Codex explains:

Read More

On subdirectory installs, $domain is the same as the main site’s domain, and the path is the subdirectory name (eg ‘example.com’ and ‘/blog1/’). On subdomain installs, $domain is the new subdomain + root domain (eg ‘blog1.example.com’), and $path is ‘/’.

So I would need a way to find out if the multisite is a subdirectory or subdomain install.

Related posts

Leave a Reply

2 comments

  1. You can use wp_get_sites() to get information about your blogs. It will return an array containing information about all existing blogs.

    Example result from the documentation:

    Array(
        [0] => Array(
            [blog_id] => 1
            [site_id] => 1
            [domain] => example.com
            [path] => /
            [registered] => 2013-11-08 17:56:46
            [last_updated] => 2013-11-08 18:57:19
            [public] => 1
            [archived] => 0
            [mature] => 0
            [spam] => 0
            [deleted] => 0
            [lang_id] => 0
        )
    
        [1] => Array(
            [blog_id] => 2
            [site_id] => 1
            [domain] => example.com
            [path] => /examplesubsite/
            [registered] => 2013-11-08 18:07:22
            [last_updated] => 2013-11-08 18:13:40
            [public] => 1
            [archived] => 0
            [mature] => 0
            [spam] => 0
            [deleted] => 0
            [lang_id] => 0
        )
    )
    

    [0] will be your main blog, so I’d ignore this. But by checking the first blog after that you should be able to get the information.

    $info = wp_get_sites();
    if ($info[1]['path'] == "/") {
        // the installation uses subdomains
    }
    else {
        // the installation uses subdirectorys
    }
    

    Of course this only works when there is at least one additional blog created, but I couldn’t find any other option to check for.