get_id_from_blogname

The timeline below displays how wordpress function get_id_from_blogname has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Retrieves a site's ID given its (subdomain or directory) slug.
 *
 * @since MU (3.0.0)
 * @since 4.7.0 Converted to use `get_sites()`.
 *
 * @param string $slug A site's slug.
 * @return int|null The site ID, or null if no site is found for the given slug.
 */
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, 'update_site_meta_cache' => false));
    if (empty($site_ids)) {
        return null;
    }
    return array_shift($site_ids);
}

WordPress Version: .10

/**
 * Retrieves a sites ID given its (subdomain or directory) slug.
 *
 * @since MU (3.0.0)
 * @since 4.7.0 Converted to use `get_sites()`.
 *
 * @param string $slug A site's slug.
 * @return int|null The site ID, or null if no site is found for the given slug.
 */
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, 'update_site_meta_cache' => false));
    if (empty($site_ids)) {
        return null;
    }
    return array_shift($site_ids);
}

WordPress Version: 5.1

/**
 * Retrieves a sites ID given its (subdomain or directory) slug.
 *
 * @since MU (3.0.0)
 * @since 4.7.0 Converted to use `get_sites()`.
 *
 * @param string $slug A site's slug.
 * @return int|null The site ID, or null if no site is found for the given slug.
 */
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);
}

WordPress Version: 4.9

/**
 * Retrieves a sites ID given its (subdomain or directory) slug.
 *
 * @since MU (3.0.0)
 * @since 4.7.0 Converted to use get_sites().
 *
 * @param string $slug A site's slug.
 * @return int|null The site ID, or null if no site is found for the given slug.
 */
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);
}

WordPress Version: 4.7

/**
 * Retrieves a sites ID given its (subdomain or directory) slug.
 *
 * @since MU
 * @since 4.7.0 Converted to use get_sites().
 *
 * @param string $slug A site's slug.
 * @return int|null The site ID, or null if no site is found for the given slug.
 */
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);
}

WordPress Version: 4.4

/**
 * Given a blog's (subdomain or directory) slug, retrieve its id.
 *
 * @since MU
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $slug
 * @return int A blog id
 */
function get_id_from_blogname($slug)
{
    global $wpdb;
    $current_site = get_current_site();
    $slug = trim($slug, '/');
    $blog_id = wp_cache_get('get_id_from_blogname_' . $slug, 'blog-details');
    if ($blog_id) {
        return $blog_id;
    }
    if (is_subdomain_install()) {
        $domain = $slug . '.' . $current_site->domain;
        $path = $current_site->path;
    } else {
        $domain = $current_site->domain;
        $path = $current_site->path . $slug . '/';
    }
    $blog_id = $wpdb->get_var($wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path));
    wp_cache_set('get_id_from_blogname_' . $slug, $blog_id, 'blog-details');
    return $blog_id;
}

WordPress Version: 4.3

/**
 * Given a blog's (subdomain or directory) slug, retrieve its id.
 *
 * @since MU
 *
 * @global wpdb $wpdb
 *
 * @param string $slug
 * @return int A blog id
 */
function get_id_from_blogname($slug)
{
    global $wpdb;
    $current_site = get_current_site();
    $slug = trim($slug, '/');
    $blog_id = wp_cache_get('get_id_from_blogname_' . $slug, 'blog-details');
    if ($blog_id) {
        return $blog_id;
    }
    if (is_subdomain_install()) {
        $domain = $slug . '.' . $current_site->domain;
        $path = $current_site->path;
    } else {
        $domain = $current_site->domain;
        $path = $current_site->path . $slug . '/';
    }
    $blog_id = $wpdb->get_var($wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path));
    wp_cache_set('get_id_from_blogname_' . $slug, $blog_id, 'blog-details');
    return $blog_id;
}

WordPress Version: 3.8

/**
 * Given a blog's (subdomain or directory) slug, retrieve its id.
 *
 * @since MU
 *
 * @param string $slug
 * @return int A blog id
 */
function get_id_from_blogname($slug)
{
    global $wpdb;
    $current_site = get_current_site();
    $slug = trim($slug, '/');
    $blog_id = wp_cache_get('get_id_from_blogname_' . $slug, 'blog-details');
    if ($blog_id) {
        return $blog_id;
    }
    if (is_subdomain_install()) {
        $domain = $slug . '.' . $current_site->domain;
        $path = $current_site->path;
    } else {
        $domain = $current_site->domain;
        $path = $current_site->path . $slug . '/';
    }
    $blog_id = $wpdb->get_var($wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path));
    wp_cache_set('get_id_from_blogname_' . $slug, $blog_id, 'blog-details');
    return $blog_id;
}

WordPress Version: 3.7

/**
 * Given a blog's (subdomain or directory) slug, retrieve its id.
 *
 * @since MU
 *
 * @param string $slug
 * @return int A blog id
 */
function get_id_from_blogname($slug)
{
    global $wpdb, $current_site;
    $slug = trim($slug, '/');
    $blog_id = wp_cache_get('get_id_from_blogname_' . $slug, 'blog-details');
    if ($blog_id) {
        return $blog_id;
    }
    if (is_subdomain_install()) {
        $domain = $slug . '.' . $current_site->domain;
        $path = $current_site->path;
    } else {
        $domain = $current_site->domain;
        $path = $current_site->path . $slug . '/';
    }
    $blog_id = $wpdb->get_var($wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path));
    wp_cache_set('get_id_from_blogname_' . $slug, $blog_id, 'blog-details');
    return $blog_id;
}