How to get a list of mapped domains?

How do I retrieve a full list (preferably with no protocol prefixes) of domains mapped across the Multisite? It should be just a clear list of domains like

domain1.com
domain2.com
domain3.com

and so on.

The goal is to get a simple, clean list that could then be used e.g. to mass-add the programmatically to the Cloudflare account or use with any other API etc.

  • Ash

    Hello Ben

    Please try the following code:

    function get_all_mapped_domains()
    {
    	global $wpdb;
    
        $prefix = isset( $wpdb->base_prefix ) ? $wpdb->base_prefix : $wpdb->prefix;
    	if( ! defined( 'DOMAINMAP_TABLE_MAP' ) ) define( 'DOMAINMAP_TABLE_MAP', "{$prefix}domain_mapping" );
    
    	$q = "
    		SELECT SQL_CALC_FOUND_ROWS mapped.domain AS mapped_domain, blog.<code>blog_id</code>, blog.<code>domain</code>, mapped.<code>is_primary</code>,  mapped.<code>scheme</code>, mapped.<code>active</code>, blog.<code>site_id</code>
    		  FROM " . DOMAINMAP_TABLE_MAP . " AS mapped
    		  LEFT JOIN {$wpdb->blogs} AS blog ON mapped.blog_id = blog.blog_id
    		 ORDER BY blog.blog_id DESC
    		";
    
    	return $wpdb->get_results( $q, OBJECT );
    }

    Then use this function to get an array of object that will contain the mapped domain list: get_all_mapped_domains

    Hope it helps! Please feel free to ask more questions if you have any.

    Have a nice day!

    Cheers,
    Ash