BuddyDev

Search

Cloning Blogs on WordPress Multisite Programmatically

It has been a long time since I posted here. Today, I am going to show you the simplest way to clone a blog on WordPress multisite programmatically.

Cloning is the process of creating identical blogs from a specified blog. In case of WordPress mutisite, we may want to preserve some of the aspects of the blog in which we are cloning the info. For example, we do not want to clone the exact site name or description. Also, we do not want to play with the users roles etc.

Here is a small function that clones everything from the specified blog to a target blog ( The blog in which we are copying data )


/**
 *
 * @global type $wpdb
 * @param int $clone_from_blog_id the blog id which we are going to clone
 * @param int $clone_to_blog_id the blog id in which we are cloning
 */
function bpdev_clone_blog( $clone_from_blog_id, $clone_to_blog_id ){

 global $wpdb;

 //the table prefix for the blog we want to clone
 $old_table_prefix = $wpdb->get_blog_prefix( $clone_from_blog_id );

 //the table prefix for the target blog in which we are cloning
 $new_table_prefix = $wpdb->get_blog_prefix( $clone_to_blog_id );

 //which tables we want to clone
 //add or remove your table here
 $tables = array( 'posts', 'comments', 'options', 'postmeta',
 'terms', 'term_taxonomy', 'term_relationships', 'commentmeta' );

 //the options that we don't want to alter on the target blog
 //we will preserve the values for these in the options table of newly created blog
 $excluded_options = array(

 'siteurl',
 'blogname',
 'blogdescription',
 'home',
 'admin_email',
 'upload_path',
 'upload_url_path',
 $new_table_prefix.'user_roles' //preserve the roles
 //add your on keys to preserve here
 );

 //should we? I don't see any reason to do it, just to avoid any glitch
 $excluded_options = esc_sql( $excluded_options );

 //we are going to use II Clause to fetch everything in single query. For this to work, we will need to quote the string
 //
 //not the best way to do it, will improve in future
 //I could not find an elegant way to quote string using sql, so here it is
 $excluded_option_list = "('" . join( "','", $excluded_options ) . "')";

 //the options table name for the new blog in which we are going to clone in next few seconds
 $new_blog_options_table = $new_table_prefix.'options';

 $excluded_options_query = "SELECT option_name, option_value FROM {$new_blog_options_table} WHERE option_name IN {$excluded_option_list}";

 //let us fetch the data

 $excluded_options_data = $wpdb->get_results( $excluded_options_query );

 //we have got the data which we need to update again later

 //now for each table, let us clone
 foreach( $tables as $table ){

 //drop table
 //clone table
 $query_drop = "DROP TABLE {$new_table_prefix}{$table}";

 $query_copy = "CREATE TABLE {$new_table_prefix}{$table} AS (SELECT * FROM {$old_table_prefix}{$table})" ;
 //drop table
 $wpdb->query( $query_drop );
 //clone table
 $wpdb->query( $query_copy );

 }

 //update the preserved options to the options table of the clonned blog
 foreach( (array) $excluded_options_data as $excluded_option ){

 update_blog_option( $clone_to_blog_id, $excluded_option->option_name, $excluded_option->option_value );
 }

}

To clone one blog into another we need following information:-

  • The ID of the blog which we want to clone or mirror
  • The ID of the blog in Which we are cloning the info

The above function provides following features:-

  • Clones One blog to another blog on a Multisite
  • Preserves the target blog title, description, user roles
  • The cloned blog retains active plugins, themes, widgets etc exactly same as the original blog from which we are cloning

Usage Example:-

Example 1:  Cloning Blog ID 2 to Blog ID 5


bpdev_clone_blog( 2, 5 );// everything from blog 2 is cloned to blog 5 including active plugins, themes etc. Only the title, description, user roles of blog 5 will be preserved.

Example 2:- Clone everything from a specific blog to the newly registered blog when a new blog is registered


add_action( 'wpmu_new_blog', 'bpdev_clone_on_new_blog_registration',10, 6 );
function bpdev_clone_on_new_blog_registration( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {

 $specified_blog_id = 2;//change it to the blog you want to clone
 bpdev_clone_blog( $specified_blog_id, $blog_id ); //clone the specified blog to the newly registered blog

}

That’s it. You can use the functions in your own plugin or put it in bp-custom.php if you are using BuddyPress. Hope that helps.

Is it helpful? Are you going to use it? Should we improve?. Please do let me know your suggestions and feedback in the comment below :)

Get latest WordPress & BuddyPress tutorials, news and tricks in your inbox.

We send out a lovely email newsletter with the most popular stories. Be a part of it.

2 Responses to Cloning Blogs on WordPress Multisite Programmatically

Leave a Reply

Are you looking for support?

Please join BuddyDev Premium Club to get timely support. At the moment, We are unable to provide much support in comments. By Joining BuddyDev Premium Club, you will get guaranteed support(and we have 30 days money back guarantee too).

Your email address will not be published. All the fields marked as * are required.


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>