[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/network/ -> site-new.php (source)

   1  <?php
   2  /**
   3   * Add Site Administration Screen
   4   *
   5   * @package WordPress
   6   * @subpackage Multisite
   7   * @since 3.1.0
   8   */
   9  
  10  /** Load WordPress Administration Bootstrap */
  11  require_once  __DIR__ . '/admin.php';
  12  
  13  /** WordPress Translation Installation API */
  14  require_once  ABSPATH . 'wp-admin/includes/translation-install.php';
  15  
  16  if ( ! current_user_can( 'create_sites' ) ) {
  17      wp_die( __( 'Sorry, you are not allowed to add sites to this network.' ) );
  18  }
  19  
  20  get_current_screen()->add_help_tab(
  21      array(
  22          'id'      => 'overview',
  23          'title'   => __( 'Overview' ),
  24          'content' =>
  25              '<p>' . __( 'This screen is for Super Admins to add new sites to the network. This is not affected by the registration settings.' ) . '</p>' .
  26              '<p>' . __( 'If the admin email for the new site does not exist in the database, a new user will also be created.' ) . '</p>',
  27      )
  28  );
  29  
  30  get_current_screen()->set_help_sidebar(
  31      '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
  32      '<p>' . __( '<a href="https://developer.wordpress.org/advanced-administration/multisite/admin/#network-admin-sites-screen">Documentation on Site Management</a>' ) . '</p>' .
  33      '<p>' . __( '<a href="https://wordpress.org/support/forum/multisite/">Support forums</a>' ) . '</p>'
  34  );
  35  
  36  if ( isset( $_REQUEST['action'] ) && 'add-site' === $_REQUEST['action'] ) {
  37      check_admin_referer( 'add-blog', '_wpnonce_add-blog' );
  38  
  39      if ( ! is_array( $_POST['blog'] ) ) {
  40          wp_die( __( 'Cannot create an empty site.' ) );
  41      }
  42  
  43      $blog   = $_POST['blog'];
  44      $domain = '';
  45  
  46      $blog['domain'] = trim( $blog['domain'] );
  47      if ( preg_match( '|^([a-zA-Z0-9-])+$|', $blog['domain'] ) ) {
  48          $domain = strtolower( $blog['domain'] );
  49      }
  50  
  51      // If not a subdomain installation, make sure the domain isn't a reserved word.
  52      if ( ! is_subdomain_install() ) {
  53          $subdirectory_reserved_names = get_subdirectory_reserved_names();
  54  
  55          if ( in_array( $domain, $subdirectory_reserved_names, true ) ) {
  56              wp_die(
  57                  sprintf(
  58                      /* translators: %s: Reserved names list. */
  59                      __( 'The following words are reserved for use by WordPress functions and cannot be used as site names: %s' ),
  60                      '<code>' . implode( '</code>, <code>', $subdirectory_reserved_names ) . '</code>'
  61                  )
  62              );
  63          }
  64      }
  65  
  66      $title = $blog['title'];
  67  
  68      $meta = array(
  69          'public' => 1,
  70      );
  71  
  72      // Handle translation installation for the new site.
  73      if ( isset( $_POST['WPLANG'] ) ) {
  74          if ( '' === $_POST['WPLANG'] ) {
  75              $meta['WPLANG'] = ''; // en_US
  76          } elseif ( in_array( $_POST['WPLANG'], get_available_languages(), true ) ) {
  77              $meta['WPLANG'] = $_POST['WPLANG'];
  78          } elseif ( current_user_can( 'install_languages' ) && wp_can_install_language_pack() ) {
  79              $language = wp_download_language_pack( wp_unslash( $_POST['WPLANG'] ) );
  80              if ( $language ) {
  81                  $meta['WPLANG'] = $language;
  82              }
  83          }
  84      }
  85  
  86      if ( empty( $title ) ) {
  87          wp_die( __( 'Missing site title.' ) );
  88      }
  89  
  90      if ( empty( $domain ) ) {
  91          wp_die( __( 'Missing or invalid site address.' ) );
  92      }
  93  
  94      if ( isset( $blog['email'] ) && '' === trim( $blog['email'] ) ) {
  95          wp_die( __( 'Missing email address.' ) );
  96      }
  97  
  98      $email = sanitize_email( $blog['email'] );
  99      if ( ! is_email( $email ) ) {
 100          wp_die( __( 'Invalid email address.' ) );
 101      }
 102  
 103      if ( is_subdomain_install() ) {
 104          $newdomain = $domain . '.' . preg_replace( '|^www\.|', '', get_network()->domain );
 105          $path      = get_network()->path;
 106      } else {
 107          $newdomain = get_network()->domain;
 108          $path      = get_network()->path . $domain . '/';
 109      }
 110  
 111      $password = 'N/A';
 112      $user_id  = email_exists( $email );
 113      if ( ! $user_id ) { // Create a new user with a random password.
 114          /**
 115           * Fires immediately before a new user is created via the network site-new.php page.
 116           *
 117           * @since 4.5.0
 118           *
 119           * @param string $email Email of the non-existent user.
 120           */
 121          do_action( 'pre_network_site_new_created_user', $email );
 122  
 123          $user_id = username_exists( $domain );
 124          if ( $user_id ) {
 125              wp_die( __( 'The domain or path entered conflicts with an existing username.' ) );
 126          }
 127          $password = wp_generate_password( 12, false );
 128          $user_id  = wpmu_create_user( $domain, $password, $email );
 129          if ( false === $user_id ) {
 130              wp_die( __( 'There was an error creating the user.' ) );
 131          }
 132  
 133          /**
 134           * Fires after a new user has been created via the network site-new.php page.
 135           *
 136           * @since 4.4.0
 137           *
 138           * @param int $user_id ID of the newly created user.
 139           */
 140          do_action( 'network_site_new_created_user', $user_id );
 141      }
 142  
 143      $wpdb->hide_errors();
 144      $id = wpmu_create_blog( $newdomain, $path, $title, $user_id, $meta, get_current_network_id() );
 145      $wpdb->show_errors();
 146  
 147      if ( ! is_wp_error( $id ) ) {
 148          if ( ! is_super_admin( $user_id ) && ! get_user_option( 'primary_blog', $user_id ) ) {
 149              update_user_option( $user_id, 'primary_blog', $id, true );
 150          }
 151  
 152          wpmu_new_site_admin_notification( $id, $user_id );
 153          wpmu_welcome_notification( $id, $user_id, $password, $title, array( 'public' => 1 ) );
 154          wp_redirect(
 155              add_query_arg(
 156                  array(
 157                      'update' => 'added',
 158                      'id'     => $id,
 159                  ),
 160                  'site-new.php'
 161              )
 162          );
 163          exit;
 164      } else {
 165          wp_die( $id->get_error_message() );
 166      }
 167  }
 168  
 169  if ( isset( $_GET['update'] ) ) {
 170      $messages = array();
 171      if ( 'added' === $_GET['update'] ) {
 172          $messages[] = sprintf(
 173              /* translators: 1: Dashboard URL, 2: Network admin edit URL. */
 174              __( 'Site added. <a href="%1$s">Visit Dashboard</a> or <a href="%2$s">Edit Site</a>' ),
 175              esc_url( get_admin_url( absint( $_GET['id'] ) ) ),
 176              network_admin_url( 'site-info.php?id=' . absint( $_GET['id'] ) )
 177          );
 178      }
 179  }
 180  
 181  // Used in the HTML title tag.
 182  $title       = __( 'Add Site' );
 183  $parent_file = 'sites.php';
 184  
 185  wp_enqueue_script( 'user-suggest' );
 186  
 187  require_once  ABSPATH . 'wp-admin/admin-header.php';
 188  
 189  ?>
 190  
 191  <div class="wrap">
 192  <h1 id="add-new-site"><?php _e( 'Add Site' ); ?></h1>
 193  <?php
 194  if ( ! empty( $messages ) ) {
 195      $notice_args = array(
 196          'type'        => 'success',
 197          'dismissible' => true,
 198          'id'          => 'message',
 199      );
 200  
 201      foreach ( $messages as $msg ) {
 202          wp_admin_notice( $msg, $notice_args );
 203      }
 204  }
 205  ?>
 206  <p><?php echo wp_required_field_message(); ?></p>
 207  <form method="post" enctype="multipart/form-data" action="<?php echo esc_url( network_admin_url( 'site-new.php?action=add-site' ) ); ?>" novalidate="novalidate">
 208  <?php wp_nonce_field( 'add-blog', '_wpnonce_add-blog' ); ?>
 209      <table class="form-table" role="presentation">
 210          <tr class="form-field form-required">
 211              <th scope="row">
 212                  <label for="site-address">
 213                      <?php
 214                      _e( 'Site Address (URL)' );
 215                      echo ' ' . wp_required_field_indicator();
 216                      ?>
 217                  </label>
 218              </th>
 219              <td>
 220                  <span class="code">
 221                      <?php if ( is_subdomain_install() ) : ?>
 222                          <input name="blog[domain]" type="text" class="regular-text code" id="site-address" aria-describedby="site-address-desc" autocapitalize="none" autocorrect="off" required /><!--
 223                          --><code class="no-break"><?php echo esc_html( '.' . preg_replace( '|^www\.|', '', get_network()->domain ) ); ?></code>
 224                      <?php else : ?>
 225                          <code class="no-break"><?php echo esc_html( get_network()->domain . get_network()->path ); ?></code><!--
 226                          --><input name="blog[domain]" type="text" class="regular-text code" id="site-address" aria-describedby="site-address-desc" autocapitalize="none" autocorrect="off" required />
 227                      <?php endif; ?>
 228                  </span>
 229                  <p class="description" id="site-address-desc">
 230                      <?php _e( 'Only lowercase letters (a-z), numbers, and hyphens are allowed.' ); ?>
 231                  </p>
 232              </td>
 233          </tr>
 234          <tr class="form-field form-required">
 235              <th scope="row">
 236                  <label for="site-title">
 237                      <?php
 238                      _e( 'Site Title' );
 239                      echo ' ' . wp_required_field_indicator();
 240                      ?>
 241                  </label>
 242              </th>
 243              <td><input name="blog[title]" type="text" class="regular-text" id="site-title" required /></td>
 244          </tr>
 245          <?php
 246          $languages    = get_available_languages();
 247          $translations = wp_get_available_translations();
 248          if ( ! empty( $languages ) || ! empty( $translations ) ) :
 249              ?>
 250              <tr class="form-field form-required">
 251                  <th scope="row"><label for="site-language"><?php _e( 'Site Language' ); ?></label></th>
 252                  <td>
 253                      <?php
 254                      // Network default.
 255                      $lang = get_site_option( 'WPLANG' );
 256  
 257                      // Use English if the default isn't available.
 258                      if ( ! in_array( $lang, $languages, true ) ) {
 259                          $lang = '';
 260                      }
 261  
 262                      wp_dropdown_languages(
 263                          array(
 264                              'name'                        => 'WPLANG',
 265                              'id'                          => 'site-language',
 266                              'selected'                    => $lang,
 267                              'languages'                   => $languages,
 268                              'translations'                => $translations,
 269                              'show_available_translations' => current_user_can( 'install_languages' ) && wp_can_install_language_pack(),
 270                          )
 271                      );
 272                      ?>
 273                  </td>
 274              </tr>
 275          <?php endif; // Languages. ?>
 276          <tr class="form-field form-required">
 277              <th scope="row">
 278                  <label for="admin-email">
 279                      <?php
 280                      _e( 'Admin Email' );
 281                      echo ' ' . wp_required_field_indicator();
 282                      ?>
 283                  </label>
 284              </th>
 285              <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" aria-describedby="site-admin-email" required /></td>
 286          </tr>
 287          <tr class="form-field">
 288              <td colspan="2" class="td-full"><p id="site-admin-email"><?php _e( 'A new user will be created if the above email address is not in the database.' ); ?><br /><?php _e( 'The username and a link to set the password will be mailed to this email address.' ); ?></p></td>
 289          </tr>
 290      </table>
 291  
 292      <?php
 293      /**
 294       * Fires at the end of the new site form in network admin.
 295       *
 296       * @since 4.5.0
 297       */
 298      do_action( 'network_site_new_form' );
 299  
 300      submit_button( __( 'Add Site' ), 'primary', 'add-site' );
 301      ?>
 302      </form>
 303  </div>
 304  <?php
 305  require_once  ABSPATH . 'wp-admin/admin-footer.php';


Generated : Tue May 5 08:20:14 2026 Cross-referenced by PHPXref