Function to return the mapped domain name?

Is there a condition and/or function I can use as a developer to get or return the domain the user mapped to a subdomain site on my network?

Ideally...
if mapped domain exists { echo get_mapped_domain($blog_ID); }

  • Tyler Postle
    • CGO

    Hey Denis,

    Hope you're doing well today and thanks for your question!

    I'm actually not sure myself whether there is a condition/function for this. What are you planning on using it for if you don't mind me asking?

    When one of our developers is online I can relay the information on to them and see if they know how it can be done :slight_smile:

    Look forward to hearing back!

    All the best,
    Tyler

  • Tyler Postle
    • CGO

    Hey Denis,

    Thanks for getting back to me with that info. I have left a note for one of our developers to take a look at this for you :slight_smile:

    Keep in mind, most of our developers take weekends off, so response times might be a little bit slower. Thanks for your patience Denis!

    Talk to you soon.

    All the best,
    Tyler

  • Timothy Bowers
    • Chief Pigeon

    Hey there,

    You could add something like this to your sites mu-plugins folder:

    function tkb_get_mapped_domain($site_id = NULL){
    	global $wpdb;
    
    	if ($site_id === NULL){
    		$site_id = get_current_blog_id();
    	}
    	$mapped_domain = $wpdb->get_var( $wpdb->prepare( "SELECT domain FROM {$wpdb->dmtable} WHERE blog_id = %d LIMIT 1", $site_id ) );
    	echo $mapped_domain;
    }

    You can then call the function like this:

    tkb_get_mapped_domain();

    If you want something other than the current site ID you can pass in another ID:

    tkb_get_mapped_domain($another_ID);

    https://gist.github.com/gmax21/771dfdacb2a083cc96e3

    Just incase you don't know what the mu folder is, it stands for Must Use, you need to manually create in your /wp-contents/ directory a new folder /mu-plugins/ and place a php file there with this code, you can then call the function from where you need in the network. :slight_smile:

    Hope this helps.

    Take care.

  • Denis Lam
    • The Incredible Code Injector

    Hi Tim, thank you for the code that has been tremendously helpful.

    Now that I can return the mapped domain based on a blog ID, I was wondering if you can provide me some code that can return that mapped domain's original "un-mapped" domain?

    This is the logic:

    function get_original_domain_from_mapped_domain($site_id) {
    if ( !empty(tkb_get_mapped_domain($site_id)) ) { // if mapped domain exists
     return original domain...
    }
    }

    So if userblog.com is mapped to user.myblog.com, then the function above would return "user.myblog.com" since its the original domain. And I can even parse the value to show "myblog.com" if desired.

    Thanks for your help once again!

  • Timothy Bowers
    • Chief Pigeon

    Don't want much do you? ha ha :slight_smile:

    You could use this:

    echo dm_site_url();

    Or you get the home url:

    echo dm_home_url()

    Just to backtrack a little here, I provided the function because you asked for that but did you also know you can return the home url like:

    $blog_title = get_bloginfo('url');

    Or site URL:

    $blog_title = get_bloginfo('wpurl');

    Take care.

  • aristath
    • Recruit

    Hello again @Denis Lam, I hope you're well today!

    No, you don't have to include anything.
    Just to be safe though, you may want to perform a check if the functions actually exist before executing them. This way even if for some reason you uninstall the plugin your site won't break.
    So instead of this:
    echo dm_site_url()
    you should use thie:

    if ( function_exists( 'dm_site_url' ) ) {
      echo dm_site_url();
    }

    Same this goes for the dm_home_url() function.

    I hope that helps!

    I'm marking this thread as resolved.

    Take care,
    Ari.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.