get_id_from_blogname

Retrieves a sites ID given its (subdomain or directory) slug.

Description

(int|null) get_id_from_blogname( (string) $slug ); 

Returns (int|null)

The site ID, or null if no site is found for the given slug.

Parameters (1)

0. $slug (string)
A site's slug.

Usage

  1. if ( !function_exists( 'get_id_from_blogname' ) ) { 
  2. require_once ABSPATH . WPINC . '/ms-blogs.php'; 
  3.  
  4. // A site's slug. 
  5. $slug = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = get_id_from_blogname($slug); 
  9.  

Defined (1)

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

/wp-includes/ms-blogs.php  
  1. function get_id_from_blogname( $slug ) { 
  2. $current_network = get_network(); 
  3. $slug = trim( $slug, '/' ); 
  4.  
  5. if ( is_subdomain_install() ) { 
  6. $domain = $slug . '.' . preg_replace( '|^www\.|', '', $current_network->domain ); 
  7. $path = $current_network->path; 
  8. } else { 
  9. $domain = $current_network->domain; 
  10. $path = $current_network->path . $slug . '/'; 
  11.  
  12. $site_ids = get_sites( array( 
  13. 'number' => 1,  
  14. 'fields' => 'ids',  
  15. 'domain' => $domain,  
  16. 'path' => $path,  
  17. ) ); 
  18.  
  19. if ( empty( $site_ids ) ) { 
  20. return null; 
  21.  
  22. return array_shift( $site_ids );