Skip to content

Instantly share code, notes, and snippets.

@soderlind
Created February 5, 2018 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soderlind/427099052d2e18e96e4d40f9bad288b8 to your computer and use it in GitHub Desktop.
Save soderlind/427099052d2e18e96e4d40f9bad288b8 to your computer and use it in GitHub Desktop.
Find WordPress subsite URL, supports Mercator and Domain Mapping plugin
<?php
$sites = array_map( 'get_object_vars', get_sites( array( 'deleted' => 0 ) ) ); //WP 4.6+
foreach ( $sites as $site ) {
switch ( $site['blog_id'] ) {
// If you are using Mercator (https://github.com/humanmade/Mercator) and domain mapping is active
case class_exists( '\Mercator\Mapping' ) && $mappings = \Mercator\Mapping::get_by_site( $site['blog_id'] ) :
foreach ( $mappings as $mapping ) {
if ( $mapping->is_active() ) {
$site_url = $mapping->get_domain();
}
}
break;
// If you are using WordPress MU Domain Mapping (https://wordpress.org/plugins/wordpress-mu-domain-mapping/)
case function_exists( 'domain_mapping_siteurl' ) && switch_to_blog( $site['blog_id'] ) :
$site_url = domain_mapping_siteurl(false);
restore_current_blog();
break;
// No domain mapping
default:
$network_blog_details = get_blog_details( $site['blog_id'] );
$site_url = $network_blog_details->home;
break;
}
echo $site_url;
}
@soderlind
Copy link
Author

soderlind commented Feb 5, 2018

Note: get_sites(), by default, only returns 100 sites. If you want all sites, use get_sites( [ 'number' => 0 ] ). You can also paginate through the sites list using get_sites( [ 'offset'' => $start, 'number' => $num ] )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment