web-dev-qa-db-fra.com

Multisite: copier le modèle lorsqu'un sous-site est créé

Je souhaite créer un site Web sur lequel les nouveaux utilisateurs enregistrés ont leur propre site Web. Le nouveau site doit avoir le contenu et certaines options activées de mon modèle. J'utilise Customizr, c'est génial.

Je n'ai pas encore changé les options initiales par défaut de Customizr. J'ai donc pensé à faire une copie automatique de mon modèle avec les données d'enregistrement (nom du sous-domaine, nom d'utilisateur, etc.) chaque fois qu'un utilisateur s'enregistre.

Je pense que NS clone a un complément, mais je ne suis pas sûr. Aussi, je préférerais le faire manuellement.

Est-ce que quelqu'un sait comment le faire?

1
Nanosergio

Enfin, je vais utiliser Multisite Cloner . Ce n'est pas pris en charge, mais cela fonctionne bien.

1
Nanosergio

J'ai automatisé https://wordpress.org/plugins/blog-copier/ pour résoudre ce problème. Si vous voulez juste le faire manuellement, prenez ce plugin sinon voici ma version automatisée:

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);

}

et la classe étendue Blog Copier est-ce

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;
        }
    }
}

Quelque chose comme ceci est la voie à suivre

0
seot