How to Get More From WordPress With Multisite Blogs (Network Install)

WordPress is surely the most used CMS in the world. Here we will talk a little about it’s incredibly easy multisite install (a.k.a. Network install). With multisite you can set up multiple blogs that share the same WordPress files, DB, plugins, themes, so you can do some cool things with it. We will see how to get posts from another blog in the network, as it was yours, create users, dynamically create a blog, how to create multiple blogs in different domains, and much more.

So, let’s rock.

What is Multisite? Why it is so good? Why should I care?

With Multisite install you can create (and remove!) multiple blogs that can work together. They share the same DB, WP files and domain (but you can use it in multiple domains, see below).

It gives you a lot of power to build complex networks with a few clicks.

For example, let’s say that as a really good freelancer you have a lot of good customers (I hope so! :D). If you use WordPress as your CMS, you have to install one blog for each one of them, right? No. If you set up a network, you just have to upload a new theme and create a new sub-blog for each one of them. Actually, you can have the same text, menus, and categories for all of them.

Advantages

Share info

You have some info that is easily shared between all blogs, so you don’t have to worry about:

  • Common user data – Users who are added to your network will have subscriber access to all sites on your network. People don’t need to have an account for each blog in the same network.
  • Posts – You can easily get posts (categories, meta-fields…) from any blog in your network and post it as if it were your own content.
  • Themes & Plugins – Any theme or plugin set available to the super admin (network administrator) can be used for any blog. So you don’t have to install the same plugin in 300 blogs

Separate some info

As each blog has to be different from its sister blogs, you have some info that is still separated, like blog configurations, selected theme, and separate site administrators.

Examples

We have a lot of applications for it:

  • User created blogs – WordPress.com
  • Multi-Themed blogs – If you want to create a network like Tutsplus.com
  • Groupon-like sites – So you can have one blog for each city, which has its own administrator.
  • Freelancers – So you can have one blog for each customer, and can easily setup a preview for them.

How to activate multisite?

Since Worpdress’s Codex has a really well explained tutorial of How to Create a Network, we won’t cover this.

How to use your new admin panel

As any WordPress admin panel, the network admin panel is really easy to use. But it isn’t so easy to find. Here is where you can access it (after your network is created, of course):

From here you can do almost anything you need.

So if you want to create a new blog, just go to Sites > New site. Once created you have a lot of options for each site, use it carefully (don’t go just changing the permalink structure for each blog, for example, it may cause some errors).

If you want to set which plugin / theme will be available for each sub-blog, use this option:

Useful snippets

Talking about code, there is a few things you must pay attention to. When you activate your network, you change some functionality of wordpress, so some functions will not work as they used to (or as you thought they should). And you have some new functions that makes your work easier.

Here is a list with much more functions for your entertainment.

Switching between blogs – switch_to_blog() function

Let’s say, for example, you own wpmultisite.com and you have WordPress (Network install) there. Inside wpmultisite.com you have /phpblog and /cssblog. Your wordpress’s structure is:

  • wpmultisite.com/ – The main site, because it was installed first. Its ID is always 1
  • wpmultisite.com/phpblog – The second blog installed, so its ID is 2
  • wpmultisite.com/cssblog – The third blog installed, so its ID is 3

When a page is requested in any of these blogs, your server executes it as if it was a single wordpress install, the only difference is that it will only get data from the current blog ID.

But you can change it when you use the switch_to_blog() function. It makes your blog get data as if it were other blog in your network. So if you want to make phpblog get cssblog’s data, you just have to set switch_to_blog(3). Note that it won’t work for plugins, which is only called when you really access a blog.

You must have been asking yourself now, “how can I make this STOP?. Well, once you’ve finished you just have to call restore_current_blog() and make things go back to normal. If you called switch_to_blog() twice, each restore_current_blog() will just bring you to previous active blog. For example, if you have switch_to_blog(2) and INSIDE of it you do switch_to_blog(3) when you call restore_current_blog() it will bring you back to blog 2, and if you call restore_current_blog() again then you will be in your original blog.

Other important thing to notice is the $blog_id global variable, that stores the current rendering blog. It is useful, for example, if you have the same theme for multiple blogs inside your network.

A little example, how to get posts from phpblog if you are in root, from cssblog if you are in phpblog and from root if you are in cssblog

//gets active blog
	global $blog_id;

	switch($blog_id) {
		case 1:
			//we are in root, so we want to go to phpblog
			$goto = 2;
			break;
		case 2 :
			//we are in phpblog, so we want to go to cssblog
			$goto = 3;
			break;
		case 3 :
			//we are in cssblog, so we want to go to root
			$goto = 1;
			break;
	}

	switch_to_blog($goto); //magic!
		//I always indent code inside a switched block, it is easier to read after a few days (or months)
		$lastposts = get_posts('numberposts=-1');
		if (!empty($lastposts)) {
			foreach($lastposts as $post) :
				setup_postdata($post);
				//do some cool stuff in here
			endforeach;
		}
	restore_current_blog();
?>

Get posts from all blogs – get_last_updated()

What if you want to get some recent posts from last updated blogs, not only one blog? Then get_last_updated() is the function that will save your life!

It returns the last updated blogs as an array, so all you have to do is a foreach and you are done!

Let’s see a simple example that shows last post from last updated blogs:

$blogs = get_last_updated();
echo '
<h1>Last posts in network</h1>
';
foreach ($blogs AS $blog) {
	echo "
<h2>".$blog["domain"].$blog["path"]."</h2>
";
	switch_to_blog($blog["blog_id"]);
		$lastposts = get_posts('numberposts=1');
		foreach($lastposts as $post) :
			setup_postdata($post);
			the_title();
		endforeach;
	restore_current_blog();
}

Dynamically created blogs – wpmu_create_blog()

This is definitely my favorite. With wpmu_create_blog() you can do something similar to wordpress.com itself, where each user can create his own blog.

But before creating a blog you must create / choose the admin user. So, after you have user_id it is just call wpmu_create_blog() with its parameters. Pay attention in the “path” parameter, it must have “/” before it, or you blog will be created in an address that can’t be accessed (in our example, without “/” it would be wpmultisite.com1wd).

	$user_id = wp_create_user( $user, $pass, $mail );
	//params: wpmu_create_blog($domain, $path, $title, $user_id, $meta, $site_id)
	$id = wpmu_create_blog("www.wpmultisite.com";, "/1wd", "1WD inside wpmultisite", $user_id);
	echo $id;
?>

Are you hungry yet?

I hope you’re still hungry to learn more about creating a multisite blog. I’m sure you will use it for some great projects, you can read more about it  here:

Rochester Oliveira

I'm a web designer and entrepreneur from Itajubá (MG), Brasil. I love writing about obscure topics and doing some cool stuff. And also I do some FREE stuff, check it out: http://www.roch.com.br/

15 Smart Tools To Help You Build Your Freelance Business

Discover the awesome tools we use in making our clients comfortable and happy in learning new things every day.

Download Now

Comments

  1. says

    Hello Rochester Oliveira,

    I have problem using “Get posts from all blogs” code. That code working fine, but i want get post title permalink and post excerpt.

    Can you coding this for me?

    Thank you for great post!

  2. Ryan Smith says

    Is it possible to make the $blogs = get_last_updated(); function into a “feed” of all of the posts by all blogs on network? I have it set up as such, but I cant figure out how to display all of the posts by each blog. If I set get_posts(‘numberposts=1′); to something greater, it displays more from the blog, but not mixed in with the others in the feed. I want all of the posts in order by entry date. Here is my current code:

  3. Amanda says

    Hi there, I’ve been trying to get all the recent posts from my multisite wordpress website to show on the home page of the root blog. The get_last_updated() function would do this? I’m going to ask a stupid question now, but I really don’t know the answer! LOL. In which page/file do I put the get_last_updated() function code?
    Thanks so much!

  4. Darren says

    If you’re running a load of sites, then multi wordpress is killer.

    Just the plugin availability alone saves some boring-ass headaches!

  5. Vnu says

    Rochester Oliveira ,

    I think the last function is not needed to use manually in newer versions. Can you please look at it

    Thanks !

  6. Walter says

    i tried once a time to setup a network with wordpress blogs, but without succes. But now i will try harder.

  7. Walter says

    It’s been a pleasure reading your blog. I have bookmarked your website so that I can come back & read more in the future as well. Please do keep up the quality writing.

  8. says

    As far as I understand a multinodular you understand the creation of blogs with domains 3 levels? How might this affect otimizatsii entire blog in general?

  9. Juan says

    I was not aware that it’s possible to create multiple blogs that can work together. Thanks for this information. This is one of the most useful articles I’ve read since it doesn’t only shows the great use of wordpress but it doesn’t discriminate in anything. In other words the content of the article is all positive and it is a great start of people would get a chance to read this.

    Great work!

    Juan

    • Rochester Oliveira says

      Hey Ruan,
      I’m really glad to hear that. It is awsome that I can share a little bit of information, and it is much more to go!

      Thank you!
      []’s
      Rochester

  10. Andrea_R says

    The switch_to_blog function is actually a very expensive query to use on the front end and is not recommended for use if you have a lot of blogs in the network. (I’ve been writing about this since it was wordpress mu. ;) )

    • Rochester Oliveira says

      Hi Andrea,
      Yeah, it is deprecated too, but I don’t know another way to do anything similar, do you know?
      Thank you!