, ,


WordPress offers a very good feature for managing multiple sites from a single wordpress installation. You can do so by having multisite set up on your main wordpress site. Though this does most of the tasks for you like creating sample post and pages, assigning theme and many more. But this may not be all you need. You may prefer to have some additional functionalities when this new site is created. We will take a small scenario and will further discuss how to achieve the goal.

– Assigning a different theme on new site creation. Let’s say there are 2 themes (twentythirteen and twentytwelve) available on your wordpress main site. Twentythirteen is the default one and you want to assign twentytwelve as the default theme to the new site.

Current Functionality:
– Whenever you create a new site, it assigns the default theme to the new site. This is enough if you are trying to build just a simple. But can a new theme be assigned during this process?

– We will need to know what is the hook fired when a new site is created on a multisite set up.
– The hook we will be using is wpmu_new_blog. We will be using the following code to assign a user defined function to the above hook

// Add hook to the new blog/subsite creation
add_action('wpmu_new_blog', 'mfs_new_blog');

– Then we will define the function as below

* Add functionality for switching to a different theme after subsite creation
* @since Twenty Thirteen 1.0
* @param $blog_id
* @param $user_id
* @param $domain
* @param $path
* @param $site_id
* @param $meta
* @return void
function mfs_new_blog( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {
// Switch the newly created blog
switch_to_blog( $blog_id );

// Change to a different theme
switch_theme( 'twentytwelve' );

// Restore to the current blog

a) We need to first switch to the newly create blog. The function has this value as a parameter. So we will use switch_to_blog function with the blog id which we need to to switch to.

b) Once switched, we are now dealing with the subsite. Here we will use the switch_theme function to switch to any other theme. Here we are using ‘twentytwelve’ as we intend to assign this theme to the newly created site.

c) Once done, we should get back to the original main site. We will do so by using restore_current_blog function which will simply switch back to the main site.

That’s it, all done now. You can open the new site and see if this has really assigned the different theme or not.