Skip to content

Instantly share code, notes, and snippets.

@kellenmace
Created August 19, 2016 13:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellenmace/440eaf57c384b2c811c7f0d7348c4fd1 to your computer and use it in GitHub Desktop.
Save kellenmace/440eaf57c384b2c811c7f0d7348c4fd1 to your computer and use it in GitHub Desktop.
Get Root Domain in WordPress
<?php
/**
* Get the root domain of the site/network.
*
* @return string|bool The root domain or false on failure.
*/
function km_get_root_domain() {
$url_parts = parse_url( km_get_main_site_url() );
if ( $url_parts && isset( $url_parts['host'] ) ) {
return $url_parts['host'];
}
return false;
}
/*
* Get the home URL of the main site.
*
* @return string The home URL of the main site.
*/
function km_get_main_site_url() {
// This is the current network's information; 'site' is old terminology.
global $current_site;
if ( is_multisite() && $current_site ) {
$main_site_blog_id = $current_site->blog_id;
return get_home_url( $main_site_blog_id );
}
return home_url();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment