Using WordPress ‘get_blogaddress_by_name()’ PHP function

The get_blogaddress_by_name() WordPress PHP function retrieves the full URL of a blog, given its name.

Usage

To use the get_blogaddress_by_name() function, simply pass the blog name as a string:

$blog_url = get_blogaddress_by_name('my_blog_name');
echo $blog_url; // Outputs the full URL of the blog

Parameters

  • $blogname (string) – The name of the subdomain or directory for the blog.

More information

See WordPress Developer Resources: get_blogaddress_by_name()

Examples

Display the URL of a blog

This example demonstrates how to display the URL of a specific blog.

$blog_name = 'tech_blog';
$blog_url = get_blogaddress_by_name($blog_name);
echo "The URL of the Tech Blog is: " . $blog_url;

Redirect to another blog

This example shows how to redirect the user to another blog within the same WordPress network.

$blog_name = 'news_blog';
$blog_url = get_blogaddress_by_name($blog_name);
header('Location: ' . $blog_url);
exit;

Create a list of blog URLs

This example demonstrates how to create an unordered list of blog URLs.

$blog_names = array('blog_one', 'blog_two', 'blog_three');
echo '<ul>';
foreach ($blog_names as $name) {
    $url = get_blogaddress_by_name($name);
    echo '<li><a href="' . $url . '">' . $name . '</a></li>';
}
echo '</ul>';

Check if a blog URL is valid

This example shows how to check if a blog URL is valid by comparing it to the expected URL.

$blog_name = 'sports_blog';
$expected_url = 'https://example.com/sports_blog';
$blog_url = get_blogaddress_by_name($blog_name);

if ($blog_url === $expected_url) {
    echo 'The blog URL is valid!';
} else {
    echo 'The blog URL is not valid!';
}

Display blog URL with custom text

This example demonstrates how to display the blog URL with custom text as a link.

$blog_name = 'travel_blog';
$blog_url = get_blogaddress_by_name($blog_name);
$custom_text = 'Check out our Travel Blog!';
echo '<a href="' . $blog_url . '">' . $custom_text . '</a>';