get_id_from_blogname()
Gets the site (blog) ID by the specified slug: subdomain or directory name.
Uses: get_sites()
No Hooks.
Returns
Int|null. Site ID or null if the site could not be determined by the specified slug.
Usage
get_id_from_blogname( $slug );
- $slug(string) (required)
Subdomain or directory name of the site (subsite).
In $slug you need to specify:
- The slug of the subdomain if subsites are subdomains. For example, blog from blog.site.com.
- The slug of the directory if subsites are directories. For example, blog from site.com/blog/.
Examples
#1 Get the network site ID from the name (slug) of the site
Suppose the ID of the desired site is 2, then:
// the site can have the following domain or path, depends on the multisite settings: // blog.site.com // site.com/blog get_id_from_blogname( 'blog' ); //> 2
#2 Switching blogs based on the blogs name ($slug)
$slug = 'another-site'; $id = get_id_from_blogname( $slug ); switch_to_blog( $id ); // Do your staff restore_current_blog();
Changelog
| Since 3.0.0 | Introduced. |
| Since 4.7.0 | Converted to use get_sites(). |