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
if ( !function_exists( 'get_id_from_blogname' ) ) { require_once ABSPATH . WPINC . '/ms-blogs.php'; } // A site's slug. $slug = ''; // NOTICE! Understand what this does before running. $result = get_id_from_blogname($slug);
Defined (1)
The function is defined in the following location(s).
- /wp-includes/ms-blogs.php
- function get_id_from_blogname( $slug ) {
- $current_network = get_network();
- $slug = trim( $slug, '/' );
- if ( is_subdomain_install() ) {
- $domain = $slug . '.' . preg_replace( '|^www\.|', '', $current_network->domain );
- $path = $current_network->path;
- } else {
- $domain = $current_network->domain;
- $path = $current_network->path . $slug . '/';
- }
- $site_ids = get_sites( array(
- 'number' => 1,
- 'fields' => 'ids',
- 'domain' => $domain,
- 'path' => $path,
- ) );
- if ( empty( $site_ids ) ) {
- return null;
- }
- return array_shift( $site_ids );
- }