WordPress

Setup A WordPress Multisite

One of the best things about using WordPress is the multisite feature. This was introduced in WordPress version 3.0 and gives you ability to create a network of sites on the same install of WordPress. There are many other CMS's that provide this feature but WordPress makes them very easy to manage by allowing you to switch between different sites, share themes and share plugins.

When you have turned on the multisite feature in WordPress the end user will now have the ability to create a brand new site whenever they want. You can customise the URL that you want to give the new site. As this runs off the same install of WordPress you can choose to either install the network sites on a different sub-domain or within a sub directory.

As these new sites use the same WordPress instance they are considered to be virtual sites as they do not have their own sub-directories, but they are given a blog ID in the database which is used to get the posts information for this site. When a new site is created WordPress will create a number of tables and prefix these with the new blog ID.

Configure WordPress For Multisite

By default WordPress is installed as a single site to enable multisite you need to add a new constant in the wp-config.php.

Open the wp-config.php file and add the following line above the comment /* That's all, stop editing! Happy blogging. */ and make sure it's above the line where we require wp-settings.php file.

/* Multisite */
define( 'WP_ALLOW_MULTISITE', true );

The above has enabled you to turn on network sites, to setup your network you need to navigate to the admin area and under the tools menu you will notice there is now a network setup link.

network-setup

This is where you will decide if you want to install your new network sites as a sub-directory or on sub-domains. It doesn't matter too much to WordPress which one you choose it's more about how you want to structure your site, with it's content and with the SEO of your site.

If you choose sub-domains then search engines will treat this as a brand new site which is separate but related to the main site. If you choose sub-folders then search engines will see this as part of the same site and will treat it that way in the search results.

Sub Domains

If you choose to use sub-domains for your network sites then you will need to add some code to your wp-config.php and to your htaccess file. Adding this code will allow WordPress to redirect you to the correct content.

Open your wp-config.php file and copy the following code above where you put the constant variable WP_ALLOW_MULTISITE.

define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', true);
define('DOMAIN_CURRENT_SITE', 'test-site.com');
define('PATH_CURRENT_SITE', '/');
define('SITE_ID_CURRENT_SITE', 1);
define('BLOG_ID_CURRENT_SITE', 1);

This will tell WordPress that you have multisite setup and it will use sub-domains as the other site URLs.

Open your htaccess file and add the following code.

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]

Sub Folders

If you choose to install the network site in sub folders then you will still need to change the wp-config.php file and the htaccess file.

Open your wp-config.php file and add the following code by the WP_ALLOW_MULTISTE constant.

define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', false);
define('DOMAIN_CURRENT_SITE', 'test-site.com');
define('PATH_CURRENT_SITE', '/');
define('SITE_ID_CURRENT_SITE', 1);
define('BLOG_ID_CURRENT_SITE', 1);

In your htaccess file you need to replace the WordPress redirect with the following code.

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

New Network Sites

When you have enabled multisite and add the correct code to your wp-config.php and your htaccess file then you will just need to log out and log back in, then you will see a new link on your toolbar for your network admin settings, from here you will be able to create and manage all the sites on your network.

network-admin-link

From this menu you will be able to switch to any site in your network and start writing content. You can also manage what themes and plugins this site has available and what users can login to the different sites.

You are able to share all your themes and all your plugins across the different sites, so you can make all the site have exactly the same look and functionality and will just have to manage the themes and plugins in one place.

By default you can not share content across sites, menus, widgets or sidebars, but there are ways which you can do this. In future tutorials I will go over ways how you can customise WordPress to share content, menus and reuse sidebars on other sites.

Development With Multisites

When developing on a site that would be used for WordPress multisites you now have options to use a lot of new multisite functions. This will allow you to switch between different sites, get site options, set site options, change the primary site, set admin notices or get certain site information.

One of the most useful functions I tend to use when working with multisites is the switch_to_blog() which simply allow you to switch to a new blog in the code by passing in the site ID into the function. This function then changes the $wpdb global object which is used to tell WordPress which database tables to use to get it's content.

Here is an example of code of how you can display content in one site, switch to another and then restore the $wpdb back to the original site.

// currently displaying content for site 1
switch_to_blog( 2 );

// Display content for site 2

// switch back to display content for site 1
restore_current_blog();
Back to top