Created
February 5, 2018 15:54
-
-
Save soderlind/427099052d2e18e96e4d40f9bad288b8 to your computer and use it in GitHub Desktop.
Find WordPress subsite URL, supports Mercator and Domain Mapping plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note:
get_sites()
, by default, only returns 100 sites. If you want all sites, useget_sites( [ 'number' => 0 ] )
. You can also paginate through the sites list usingget_sites( [ 'offset'' => $start, 'number' => $num ] )