3 * Add Site Administration Screen
6 * @subpackage Multisite
10 /** Load WordPress Administration Bootstrap */
11 require_once( dirname( __FILE__ ) . '/admin.php' );
13 /** WordPress Translation Install API */
14 require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
16 if ( ! is_multisite() )
17 wp_die( __( 'Multisite support is not enabled.' ) );
19 if ( ! current_user_can( 'manage_sites' ) )
20 wp_die( __( 'You do not have sufficient permissions to add sites to this network.' ) );
22 get_current_screen()->add_help_tab( array(
24 'title' => __('Overview'),
26 '<p>' . __('This screen is for Super Admins to add new sites to the network. This is not affected by the registration settings.') . '</p>' .
27 '<p>' . __('If the admin email for the new site does not exist in the database, a new user will also be created.') . '</p>'
30 get_current_screen()->set_help_sidebar(
31 '<p><strong>' . __('For more information:') . '</strong></p>' .
32 '<p>' . __('<a href="https://codex.wordpress.org/Network_Admin_Sites_Screen" target="_blank">Documentation on Site Management</a>') . '</p>' .
33 '<p>' . __('<a href="https://wordpress.org/support/forum/multisite/" target="_blank">Support Forums</a>') . '</p>'
36 if ( isset($_REQUEST['action']) && 'add-site' == $_REQUEST['action'] ) {
37 check_admin_referer( 'add-blog', '_wpnonce_add-blog' );
39 if ( ! is_array( $_POST['blog'] ) )
40 wp_die( __( 'Can’t create an empty site.' ) );
42 $blog = $_POST['blog'];
44 if ( preg_match( '|^([a-zA-Z0-9-])+$|', $blog['domain'] ) )
45 $domain = strtolower( $blog['domain'] );
47 // If not a subdomain install, make sure the domain isn't a reserved word
48 if ( ! is_subdomain_install() ) {
49 $subdirectory_reserved_names = get_subdirectory_reserved_names();
51 if ( in_array( $domain, $subdirectory_reserved_names ) ) {
52 wp_die( sprintf( __( 'The following words are reserved for use by WordPress functions and cannot be used as blog names: <code>%s</code>' ), implode( '</code>, <code>', $subdirectory_reserved_names ) ) );
56 $title = $blog['title'];
62 // Handle translation install for the new site.
63 if ( ! empty( $_POST['WPLANG'] ) && wp_can_install_language_pack() ) {
64 $language = wp_download_language_pack( wp_unslash( $_POST['WPLANG'] ) );
66 $meta['WPLANG'] = $language;
70 if ( empty( $domain ) )
71 wp_die( __( 'Missing or invalid site address.' ) );
73 if ( isset( $blog['email'] ) && '' === trim( $blog['email'] ) ) {
74 wp_die( __( 'Missing email address.' ) );
77 $email = sanitize_email( $blog['email'] );
78 if ( ! is_email( $email ) ) {
79 wp_die( __( 'Invalid email address.' ) );
82 if ( is_subdomain_install() ) {
83 $newdomain = $domain . '.' . preg_replace( '|^www\.|', '', $current_site->domain );
84 $path = $current_site->path;
86 $newdomain = $current_site->domain;
87 $path = $current_site->path . $domain . '/';
91 $user_id = email_exists($email);
92 if ( !$user_id ) { // Create a new user with a random password
93 $user_id = username_exists( $domain );
95 wp_die( __( 'The domain or path entered conflicts with an existing username.' ) );
97 $password = wp_generate_password( 12, false );
98 $user_id = wpmu_create_user( $domain, $password, $email );
99 if ( false === $user_id ) {
100 wp_die( __( 'There was an error creating the user.' ) );
104 * Fires after a new user has been created via the network site-new.php page.
108 * @param int $user_id ID of the newly created user.
110 do_action( 'network_site_new_created_user', $user_id );
113 $wpdb->hide_errors();
114 $id = wpmu_create_blog( $newdomain, $path, $title, $user_id, $meta, $current_site->id );
115 $wpdb->show_errors();
116 if ( ! is_wp_error( $id ) ) {
117 if ( ! is_super_admin( $user_id ) && !get_user_option( 'primary_blog', $user_id ) ) {
118 update_user_option( $user_id, 'primary_blog', $id, true );
121 $content_mail = sprintf(
122 /* translators: 1: user login, 2: site url, 3: site name/title */
123 __( 'New site created by %1$s
127 $current_user->user_login,
131 wp_mail( get_site_option('admin_email'), sprintf( __( '[%s] New Site Created' ), $current_site->site_name ), $content_mail, 'From: "Site Admin" <' . get_site_option( 'admin_email' ) . '>' );
132 wpmu_welcome_notification( $id, $user_id, $password, $title, array( 'public' => 1 ) );
133 wp_redirect( add_query_arg( array( 'update' => 'added', 'id' => $id ), 'site-new.php' ) );
136 wp_die( $id->get_error_message() );
140 if ( isset($_GET['update']) ) {
142 if ( 'added' == $_GET['update'] )
143 $messages[] = sprintf(
144 /* translators: 1: dashboard url, 2: network admin edit url */
145 __( 'Site added. <a href="%1$s">Visit Dashboard</a> or <a href="%2$s">Edit Site</a>' ),
146 esc_url( get_admin_url( absint( $_GET['id'] ) ) ),
147 network_admin_url( 'site-info.php?id=' . absint( $_GET['id'] ) )
151 $title = __('Add New Site');
152 $parent_file = 'sites.php';
154 wp_enqueue_script( 'user-suggest' );
156 require( ABSPATH . 'wp-admin/admin-header.php' );
161 <h1 id="add-new-site"><?php _e( 'Add New Site' ); ?></h1>
163 if ( ! empty( $messages ) ) {
164 foreach ( $messages as $msg )
165 echo '<div id="message" class="updated notice is-dismissible"><p>' . $msg . '</p></div>';
167 <form method="post" action="<?php echo network_admin_url( 'site-new.php?action=add-site' ); ?>" novalidate="novalidate">
168 <?php wp_nonce_field( 'add-blog', '_wpnonce_add-blog' ) ?>
169 <table class="form-table">
170 <tr class="form-field form-required">
171 <th scope="row"><label for="site-address"><?php _e( 'Site Address' ) ?></label></th>
173 <?php if ( is_subdomain_install() ) { ?>
174 <input name="blog[domain]" type="text" class="regular-text" id="site-address" aria-describedby="site-address-desc" autocapitalize="none" autocorrect="off"/><span class="no-break">.<?php echo preg_replace( '|^www\.|', '', $current_site->domain ); ?></span>
176 echo $current_site->domain . $current_site->path ?><input name="blog[domain]" type="text" class="regular-text" id="site-address" aria-describedby="site-address-desc" autocapitalize="none" autocorrect="off" />
178 echo '<p id="site-address-desc">' . __( 'Only lowercase letters (a-z) and numbers are allowed.' ) . '</p>';
182 <tr class="form-field form-required">
183 <th scope="row"><label for="site-title"><?php _e( 'Site Title' ) ?></label></th>
184 <td><input name="blog[title]" type="text" class="regular-text" id="site-title" /></td>
187 $languages = get_available_languages();
188 $translations = wp_get_available_translations();
189 if ( ! empty( $languages ) || ! empty( $translations ) ) :
191 <tr class="form-field form-required">
192 <th scope="row"><label for="site-language"><?php _e( 'Site Language' ); ?></label></th>
196 $lang = get_site_option( 'WPLANG' );
198 // Use English if the default isn't available.
199 if ( ! in_array( $lang, $languages ) ) {
203 wp_dropdown_languages( array(
205 'id' => 'site-language',
207 'languages' => $languages,
208 'translations' => $translations,
209 'show_available_translations' => wp_can_install_language_pack(),
214 <?php endif; // Languages. ?>
215 <tr class="form-field form-required">
216 <th scope="row"><label for="admin-email"><?php _e( 'Admin Email' ) ?></label></th>
217 <td><input name="blog[email]" type="email" class="regular-text wp-suggest-user" id="admin-email" data-autocomplete-type="search" data-autocomplete-field="user_email" /></td>
219 <tr class="form-field">
220 <td colspan="2"><?php _e( 'A new user will be created if the above email address is not in the database.' ) ?><br /><?php _e( 'The username and password will be mailed to this email address.' ) ?></td>
223 <?php submit_button( __('Add Site'), 'primary', 'add-site' ); ?>
227 require( ABSPATH . 'wp-admin/admin-footer.php' );