A Featured Blog Plugin for WordPress Multisite

WordPress is become one of the most popular open source blogging platforms. As of WordPress 3.0 WordPressMU also became a part of WordPress. The WordPress Multisite feature allows you to create a network of sites which run from the same WordPress installation. So one can upload a plugin or a theme to the installation and it will be available to all the sites on that WordPress network.

In this article we are going to see how to create a Featured Blog plugin which creates a widget and shortcode to display a Featured Blog on a WordPress Multisite installation.


Step 1 WordPress Multisite Installation

In case you don't have the WordPress Multisite installation done already you can download the latest WordPress installation from WordPress.org. This article lists through the steps for creating a WordPress Multisite installation. You can follow the steps to successfully install WordPress Multisite instance on your server.


Step 2 Creating a Network Plugin

Now we are going to create a network plugin called as featured blog. In your wp-content/plugins folder create a directory called featuredblog. Inside that directory create a file featuredblog.php with the following content:

This should create a network plugin for us as the Network: true is used and we should be able to see this plugin in the network admin plugin list as follows. Now we can activate this plugin for the whole network from the network admin.


Step 3 Creating Functions to Fetch Posts From Different Blogs

Function for Getting the Post From a Blog

In this function we take in the $blog_id and the number of posts we want to fetch. In this function the first thing we do is switch to the blog which is passed to the function. This is done through the WordPress function switch_to_blog. For more details check out href="http://codex.wordpress.org/WPMU_Functions/switch_to_blog">switch_to_blog in the WordPress Codex. Then we create a query depending on the number of posts which are to be displayed using the WP_Query. In case you are not already familiar with it, you can check out WP_Query in the WordPress Codex too. Then we loop through the results of WP_Query and add in a list of the post title and the post name. Then we use the restore_current_blog function to restore to the blog which was there before we switched blogs.

Function to Get Post for a List of Blogs

This function takes a list of comma separated blog IDs and the number of posts we want for each blog as input. In this function we remove any white space present in the comma separated blog list. Then it is exploded on the basis of the comma and we loop through each blog ID. Then using the previous function Featured_blog_posts_for_blog we fetch the posts for each blog. The result is created in a list and returned by this function.


Step 4 Creating a Shortcode

Now we are going to create a shortcode to display the featured blogs.

In the function Featured_blog_shortcode the arguments from the shortcode are passed. Then using the function shortcode_atts we merge the passed arguments with default values. For more details read the href="http://codex.wordpress.org/Function_Reference/shortcode_atts">shortcode_atts page in the WordPress Codex. Then we pass the comma separated blog list to the function Featured_blog_posts_for_specified_blogs which fetches the post from those blogs and returns a list.


Step 5 Using the ShortCode

To use the shortcode now we will create a new post, we will create a new post called "Featured Blogs" and then add the shortcode in its content as [FEATURED_BLOG blogids="3,1" numberofpost="2"], see also in the screen shot.

If we see the post preview now it will be seen as follows showing the featured blogs.


Step 6 Creating a Featured Blog Widget

To create a widget we will have to create a class that inherits from WP_Widget. The code for the widget is as follows:

In the constructor below

In this we add the name and the description for the widget.

In the form function

We create three text fields for

  • The title of the widget
  • The blog list
  • The number of posts per blog to be displayed

Here we merge the default values for the widget with the instance values using the function wp_parse_args. For more details on href="http://codex.wordpress.org/Function_Reference/wp_parse_args">wp_parse_args view the page in the WordPress Codex. The widget in the admin should now look as follows.

In the updated function

We update the title, blogids and numberofpost function when save is clicked by coping it in the instance. In the widget function

In this function we get the values saved for the widget instance. The title is used as the title of the widget. We get the values for the blog list and the number of posts and pass it to the function Featured_blog_posts_for_specified_blogs which returns the list of posts from each blog.

We need to register this widget

To register the widget we use the function register_widget in the function Featured_blog_widget_init which is added to the action hook 'widgets_init'.

The output of the widget will look as follows.


Conclusion

WordPress Multisite is a great platform to create a network of sites. In this plugin we saw how we can create a network plugin to display featured blogs. By creating such plugins we can enhance the WordPress Multisite platform and also provide the extra features which we need for our site network. There are also a lot of open source plugins for WordPress Multisite.

So have fun using WordPress Multisite for your network of sites!

Tags:

Comments

Related Articles