A
A
andrash_2021-04-01 14:32:52
WordPress
andrash_, 2021-04-01 14:32:52

How to create about 50 subdomains on Wordpress (Multisite)?

On the site, you need to create a lot of subdomains (moscow.test.ru, spb.test.ru, novosibirsk.ru, etc.) for product sales regions. Adding one at a time is not an option.
Is there any way to simplify the task?
Maybe you can somehow do without Multisite? All content on the sites will be the same, except for some meta-fields, which will depend on the selected city.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem Zolin, 2021-04-01
@andrashsh

Why didn't multisite suit you? Web sites can be created programmatically. Here is an example, I created subdomains for all 85 regions in this way

$cities = array(
  'mos' => 'Москва',
  'spb' => 'Санкт-Петербург',
  'nsk' => 'Новосибирск',
  'ekb' => 'Екатеринбург',
);

$pathinfo = pathinfo(home_url());
$network_id = get_current_network_id();
$user_id = get_current_user_id();

foreach ( $cities as $key => $city ) {
  
  $site_data = array(
    'domain' => $key .'.' . $pathinfo['basename'],
    'path' => '/',
    'network_id' => $network_id,
    'user_id' => $user_id,
    'title' => 'Заголовок сайта ' . $city,
    'options' => [
      'blogdescription' => 'Описание сайта ' . $city,
      'permalink_structure' => '/%category%/%postname%/',
      'template' => 'twentytwenty',
      'stylesheet' => 'twentytwenty',
      'posts_per_page' => '12',
      'active_plugins' => [
        'cyr2lat/cyr-to-lat.php',
        'query-monitor/query-monitor.php',
        'wordpress-seo/wp-seo.php',
        'wp-fastest-cache/wpFastestCache.php'
      ]
    ]
  );

  // вставляем сайт в базу данных
  $site_id = wp_insert_site( $site_data );

  // пишем ошибку/успех
  if( is_wp_error( $site_id ) ) {
    var_dump( 'Ошибка инсерта мультисайта ' . $key . ': ' . $site_id->get_error_message() );
  } else {
    var_dump( 'Мультисайт ' . $key . ' опубликован удачно!' );
  }

} // end foreach $cities

You can trick and duplicate content from the main site to subdomains also programmatically

S
Sergey Sokolov, 2021-04-01
@sergiks

A web server - NGINX or Apache - can be configured to forward requests for all subdomains to a single site.
Request subdomain sent additional. header or parameter, or in a script, and so the entire request with the source host containing the city is available.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question