get_blog_id_from_url

Get a blog's numeric ID from its URL.

Description

(int) get_blog_id_from_url( (string) $domain, (string) $path = '/' ); 

On a subdirectory installation like example.com/blog1/, $domain will be the root example.com and $path the subdirectory /blog1/.. With subdomains like blog1.example.com, $domain is blog1.example.com and $path is /.

Returns (int)

0 if no blog found, otherwise the ID of the matching blog

Parameters (2)

0. $domain (string)
The domain.
1. $path — Optional. (string) => '/'
Not required for subdomain installations.

Usage

  1. if ( !function_exists( 'get_blog_id_from_url' ) ) { 
  2. require_once ABSPATH . WPINC . '/ms-functions.php'; 
  3.  
  4. // The domain. 
  5. $domain = ''; 
  6.  
  7. // Optional. Not required for subdomain installations. 
  8. $path = '/'; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_blog_id_from_url($domain, $path); 
  12.  

Defined (1)

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

/wp-includes/ms-functions.php  
  1. function get_blog_id_from_url( $domain, $path = '/' ) { 
  2. $domain = strtolower( $domain ); 
  3. $path = strtolower( $path ); 
  4. $id = wp_cache_get( md5( $domain . $path ), 'blog-id-cache' ); 
  5.  
  6. if ( $id == -1 ) // blog does not exist 
  7. return 0; 
  8. elseif ( $id ) 
  9. return (int) $id; 
  10.  
  11. $args = array( 
  12. 'domain' => $domain,  
  13. 'path' => $path,  
  14. 'fields' => 'ids',  
  15. ); 
  16. $result = get_sites( $args ); 
  17. $id = array_shift( $result ); 
  18.  
  19. if ( ! $id ) { 
  20. wp_cache_set( md5( $domain . $path ), -1, 'blog-id-cache' ); 
  21. return 0; 
  22.  
  23. wp_cache_set( md5( $domain . $path ), $id, 'blog-id-cache' ); 
  24.  
  25. return $id;