wpmu_create_blog

Create a site.

Description

(int|WP_Error) wpmu_create_blog( (string) $domain, (string) $path, (string) $title, (int) $user_id, (array) $meta = array(), (int) $site_id = 1 ); 

This function runs when a user self-registers a new site as well as when a Super Admin creates a new site. Hook to for events that should affect all new sites.

On subdirectory installs, $domain is the same as the main site's domain, and the path is the subdirectory name (eg example.com. and /blog1/). On subdomain installs, $domain is the new subdomain + root domain (eg blog1.example.com), and $path is /.

Returns (int|WP_Error)

Returns WP_Error object on failure, int $blog_id on success

Parameters (6)

0. $domain (string)
The new site's domain.
1. $path (string)
The new site's path.
2. $title (string)
The new site's title.
3. $user_id (int)
The user ID of the new site's admin.
4. $meta — Optional. (array) => array()
Used to set initial site options.
5. $site_id — Optional. (int) => 1
Only relevant on multi-network installs.

Usage

  1. if ( !function_exists( 'wpmu_create_blog' ) ) { 
  2. require_once ABSPATH . WPINC . '/ms-functions.php'; 
  3.  
  4. // The new site's domain. 
  5. $domain = ''; 
  6.  
  7. // The new site's path. 
  8. $path = ''; 
  9.  
  10. // The new site's title. 
  11. $title = ''; 
  12.  
  13. // The user ID of the new site's admin. 
  14. $user_id = -1; 
  15.  
  16. // Optional. Used to set initial site options. 
  17. $meta = array(); 
  18.  
  19. // Optional. Only relevant on multi-network installs. 
  20. $site_id = 1; 
  21.  
  22. // NOTICE! Understand what this does before running. 
  23. $result = wpmu_create_blog($domain, $path, $title, $user_id, $meta, $site_id); 
  24.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/ms-functions.php  
  1. function wpmu_create_blog( $domain, $path, $title, $user_id, $meta = array(), $site_id = 1 ) { 
  2. $defaults = array( 
  3. 'public' => 0,  
  4. 'WPLANG' => get_site_option( 'WPLANG' ),  
  5. ); 
  6. $meta = wp_parse_args( $meta, $defaults ); 
  7.  
  8. $domain = preg_replace( '/\s+/', '', sanitize_user( $domain, true ) ); 
  9.  
  10. $domain = str_replace( '@', '', $domain ); 
  11.  
  12. $title = strip_tags( $title ); 
  13. $user_id = (int) $user_id; 
  14.  
  15. if ( empty($path) ) 
  16. $path = '/'; 
  17.  
  18. // Check if the domain has been used already. We should return anerrormessage. 
  19. if ( domain_exists($domain, $path, $site_id) ) 
  20. return new WP_Error( 'blog_taken', __( 'Sorry, that site already exists!' ) ); 
  21.  
  22. if ( ! wp_installing() ) { 
  23. wp_installing( true ); 
  24.  
  25. if ( ! $blog_id = insert_blog($domain, $path, $site_id) ) 
  26. return new WP_Error('insert_blog', __('Could not create site.')); 
  27.  
  28. switch_to_blog($blog_id); 
  29. install_blog($blog_id, $title); 
  30. wp_install_defaults($user_id); 
  31.  
  32. add_user_to_blog($blog_id, $user_id, 'administrator'); 
  33.  
  34. foreach ( $meta as $key => $value ) { 
  35. if ( in_array( $key, array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ) ) ) 
  36. update_blog_status( $blog_id, $key, $value ); 
  37. else 
  38. update_option( $key, $value ); 
  39.  
  40. update_option( 'blog_public', (int) $meta['public'] ); 
  41.  
  42. if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) ) 
  43. update_user_meta( $user_id, 'primary_blog', $blog_id ); 
  44.  
  45. /** 
  46. * Fires immediately after a new site is created. 
  47. * @since MU 
  48. * @param int $blog_id Blog ID. 
  49. * @param int $user_id User ID. 
  50. * @param string $domain Site domain. 
  51. * @param string $path Site path. 
  52. * @param int $site_id Site ID. Only relevant on multi-network installs. 
  53. * @param array $meta Meta data. Used to set initial site options. 
  54. */ 
  55. do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta ); 
  56.  
  57. wp_cache_set( 'last_changed', microtime(), 'sites' ); 
  58.  
  59. return $blog_id;