The get_blog_id_from_url() WordPress PHP function retrieves a blog’s numeric ID using its URL.
Usage
For subdirectory installs:
$blog_id = get_blog_id_from_url("example.com", "/blog1/");
For subdomain installs:
$blog_id = get_blog_id_from_url("blog1.example.com");
Parameters
- $domain (string) – Required. Website domain.
- $path (string) – Optional. Not required for subdomain installations. Default: ‘/’.
More information
See WordPress Developer Resources: get_blog_id_from_url
Examples
Subdirectory Installation
This code retrieves the blog ID for a blog installed in a subdirectory, such as example.com/blog1/
:
$domain = "example.com"; $path = "/blog1/"; $blog_id = get_blog_id_from_url($domain, $path);
Subdomain Installation
This code retrieves the blog ID for a blog installed on a subdomain, such as blog1.example.com
:
$domain = "blog1.example.com"; $blog_id = get_blog_id_from_url($domain);
Get the blog ID and display it
This code retrieves the blog ID and displays it on the page:
$domain = "example.com"; $path = "/blog1/"; $blog_id = get_blog_id_from_url($domain, $path); echo "Blog ID: " . $blog_id;
Get the blog ID for multiple blogs
This code retrieves the blog IDs for multiple blogs and stores them in an array:
$blogs = [ ["domain" => "example.com", "path" => "/blog1/"], ["domain" => "example.com", "path" => "/blog2/"], ]; $blog_ids = []; foreach ($blogs as $blog) { $blog_ids[] = get_blog_id_from_url($blog["domain"], $blog["path"]); }
Check if a blog exists
This code checks if a blog exists by trying to get its blog ID:
$domain = "example.com"; $path = "/blog1/"; $blog_id = get_blog_id_from_url($domain, $path); if ($blog_id) { echo "The blog exists!"; } else { echo "The blog does not exist."; }