get_id_from_blogname() WordPress Function

The get_id_from_blogname() function is used to retrieve the blog id given its name.

get_id_from_blogname( string $slug ) #

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


Parameters

$slug

(string)(Required)A site's slug.


Top ↑

Return

(int|null) The site ID, or null if no site is found for the given slug.


Top ↑

Source

File: 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,
			'update_site_meta_cache' => false,
		)
	);

	if ( empty( $site_ids ) ) {
		return null;
	}

	return array_shift( $site_ids );
}


Top ↑

Changelog

Changelog
VersionDescription
MU (3.0.0)MU (3.0.0)
4.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.