get main URL from subdirectory with php

I have a WordPress site that contains multiple subdirectory installations of WordPress with individual databases inside one WordPress installation, and I want to link them together in the themes. But I couldn't figure out how to dynamically get the main URL from the base WordPress directory for one of the subdirectories.

For example, I tried to use get_home_url() but it only returns https://mytestsite.com/instance1 instead of https://mytestsite.com

This might be something basic but i couldn't find anything in my resarch that could help me out.

Topic directory urls theme-development Wordpress

Category Web


site_url gets you the current sites URL, but you can get the site URL of the root blog at / via get_site_url by passing its ID, which is likely to be 1, e.g.:

$url = get_site_url( 1 );

As an aside, you may find switch_to_blog and restore_current_blog to be useful, just know that they're not cheap to call, and it won't load code from the other blog/site, if the other site relies on a custom plugin or filters it won't be present unless it's also present on the current site.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.