Multisite: copy template when a subsite is created

I want to create a website where new registered users have their own website. The new site have to have content and some options activated of my template. I'm using Customizr, it's great.

I've not get change the initial options by default of Customizr yet. So I've thought about making an automatic copy of my template with the registration data (subdomain name, username, etc.) every time a user registers.

I think NS clone has an add on for that but I'm not sure. Also, I'd prefer to do it manually.

Does anyone know how to do it?

Topic clone-site multisite Wordpress

Category Web


Finally I'll use Multisite Cloner. This is not supported but it works fine.


I automated https://wordpress.org/plugins/blog-copier/ to solve this issue. If you just want to do it manually take this plugin otherwise here is my automated version:

include "BlogCopierExtended.php";

add_action("wpmu_activate_blog", "blog_copy",15,3);

function blog_copy($blog_id)
{

    //  first I save the options I want to preserve
    $customer_name = get_blog_option($blog_id, "customer_name");

    // get the customized class
    $blog_copier = new BlogCopierExtended();

    // the default blog, from where to copy the data from
    $default_blog = get_id_from_blogname( "default" );

    // copy this
    $copy_now = $blog_copier->copy_blog_to_existing($blog_id, "", "", $default_blog, true);

    // now we save the values back
    update_blog_option($blog_id, "customer_name", $customer_name);

}

and the Blog Copier extended class is this

if (class_exists('BlogCopier')) {
    class BlogCopierExtended extends BlogCopier

    {
        public function copy_blog_to_existing($blog_id, $domain, $title, $from_blog_id = 0, $copy_files = true)
        {
            global $wpdb, $current_site, $base;
            $email = get_blog_option($from_blog_id, 'admin_email');
            $user_id = email_exists(sanitize_email($email));
            if (!$user_id) {

                // Use current user instead

                $user_id = get_current_user_id();
            }

            // The user id of the user that will become the blog admin of the new blog.

            $user_id = apply_filters('copy_blog_user_id', $user_id, $from_blog_id);
            if (is_subdomain_install()) {
                $newdomain = $domain . "." . $current_site->domain;
                $path = $base;
            }
            else {
                $newdomain = $current_site->domain;
                $path = trailingslashit($base) . trailingslashit($domain);
            }

            // The new domain that will be created for the destination blog.

            $newdomain = apply_filters('copy_blog_domain', $newdomain, $domain);

            // The new path that will be created for the destination blog.

            $path = apply_filters('copy_blog_path', $path, $domain);
            $wpdb->hide_errors();

            // $to_blog_id = get_current_blog_id();

            $to_blog_id = $blog_id;
            $wpdb->show_errors();
            if (!is_wp_error($to_blog_id)) {
                $dashboard_blog = get_dashboard_blog();
                if (!is_super_admin() && get_user_option('primary_blog', $user_id) == $dashboard_blog->blog_id) update_user_option($user_id, 'primary_blog', $to_blog_id, true);

                // now copy

                if ($from_blog_id) {
                    $this->copy_blog_data($from_blog_id, $to_blog_id);
                    if ($copy_files) {
                        $this->copy_blog_files($from_blog_id, $to_blog_id);
                        $this->replace_content_urls($from_blog_id, $to_blog_id);
                    }

                    $msg = sprintf(__('Copied: %s in %s seconds', $this->_domain) , '<a href="http://' . $newdomain . '" target="_blank">' . $title . '</a>', number_format_i18n(timer_stop()));

                    // do_action( 'log', __( 'Copy Complete!', $this->_domain ), $this->_domain, $msg );
                    //                  do_action( 'copy_blog_complete', $from_blog_id, $to_blog_id );

                }
            }
            else {
                $msg = $to_blog_id->get_error_message();
            }

            return $msg;
        }
    }
}

Something like this is the way to go

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.