get_theme_mod() function referencing subdomain instead of mapped one

We have a multisite running with the WPMU Domain Mapping plugin installed. Were currently using get_theme_mod(); function quite a bit in order to get our theme options... however when grabbing the theme mod option for an image, which outputs the url of the image, it displays the url of the subdomain and not the mapped one. How can we setup the function to reference the mapped domain instead?

It was suggested in a chat that we use the new beta version (https://premium.wpmudev.org/forums/topic/domain-mapping-beta-huge-update-sso-async-rewritten#post-864506), but we don't want to use due to our multisite being larger than most.

I was thinking we can just modify the get_theme_mod() to always reference the mapped domain instead?

  • Michelle Shull

    Hey there, BrandCo! Hope you're well today.

    I'm going to go ahead and call in our second level support team on this one to make sure we can get you the best possible answer. This is an area I'm not an expert in, and I don't want to put you on the wrong path. Our second level team is pretty busy, and they work on our more complex and time consuming issues, so it may be a just a little wait before you hear from them.

    Thanks for your question!

  • Jose

    Hi again @BrandCo,

    I was working on a custom code for you.

    I was thinking we can just modify the get_theme_mod() to always reference the mapped domain instead?

    It is always advisable to avoid any modification directly in plugin's code and, even more important, in WP core.
    So, I coded a solution that will achieve what you need without touching anything in the plugin nor the core, just hooking externally.

    You only need to download the attached zip file, and drop the .php plugin into the folder ../wp-content/mu-plugins/ (create the folder if it doesn't exists).

    My solution will overwrite the url for the header image and the background image. Let me know if I'm missing some other image in the customizer.

    Just for reference, this is the code for the mu-plugin:

    if(! class_exists('dm_theme_mod_mapping') ):
    
        class dm_theme_mod_mapping{
    
            public static $_mapped_domains = array();
    
            function __construct(){
               add_filter('theme_mod_background_image', array($this,'get_mapped_url'), 99, 1);
               add_filter('theme_mod_header_image', array($this,'get_mapped_url'), 99, 1);
            }
    
            function get_mapped_url( $url, $path = false, $orig_scheme = false, $blog_id = false ) {
                if(!function_exists('dm_site_url')){
                    return $url; //Do nothing if DM is not activated.
                }
                // parse current url
                $components = $this->parse_mb_url( $url );
    
                if ( empty( $components['host'] )) {
                    return $url;
                }
    
                // find mapped domain
                $mapped_domain = $this->get_mapped_domain( $blog_id );
                if ( !$mapped_domain || $components['host'] == $mapped_domain ) {
                    return $url;
                }
    
                $components['host'] = $mapped_domain;
    
                $mapped_url = $this->build_url( $components );
    
                return $mapped_url;
            }
    
            function parse_mb_url( $url ) {
                return array_map( 'urldecode', (array) parse_url( preg_replace_callback( '%[^:/?#&=\.]+%usD', function ($matches) {
                    return urlencode( $matches[0] );
                }, $url ) ) );
            }
    
            function fetch_mapped_domain( $blog_id ) {
                global $wpdb;
                $errors = $wpdb->suppress_errors();
                $sql    = domain_map::allow_multiple()
                    ? sprintf( "SELECT domain FROM %s WHERE blog_id = %d ORDER BY is_primary DESC, id ASC LIMIT 1", DOMAINMAP_TABLE_MAP, $blog_id )
                    : sprintf( "SELECT domain FROM %s WHERE blog_id = %d ORDER BY id ASC LIMIT 1", DOMAINMAP_TABLE_MAP, $blog_id );
                $domain = $wpdb->get_var( $sql );
                $wpdb->suppress_errors( $errors );
    
                return $domain;
            }
    
            function get_mapped_domain( $blog_id = false, $is_front = true ) {
                // use current blog id if $blog_id is empty
                if ( !$blog_id ) {
                    $blog_id = get_current_blog_id();
                }
    
                // if we have already found mapped domain, then return it
                if ( isset( self::$_mapped_domains[$blog_id] ) ) {
                    return self::$_mapped_domains[$blog_id];
                }
    
                $domain = $this->fetch_mapped_domain( $blog_id );
                // save mapped domain into local cache
                self::$_mapped_domains[$blog_id] = !empty( $domain ) ? $domain : false;
    
                return $domain;
            }
    
            function build_url( $components ) {
                $scheme = isset( $components['scheme'] ) ? $components['scheme'] . '://' : '';
                $host = isset( $components['host'] ) ? $components['host'] : '';
                $port = isset( $components['port'] ) ? ':' . $components['port'] : '';
    
                $user = isset( $components['user'] ) ? $components['user'] : '';
                $pass = isset( $components['pass'] ) ? ':' . $components['pass'] : '';
                $pass = $user || $pass ? $pass . '@' : '';
    
                $path = isset( $components['path'] ) ? $components['path'] : '';
                $query = isset( $components['query'] ) ? '?' . $components['query'] : '';
                $fragment = isset( $components['fragment'] ) ? '#' . $components['fragment'] : '';
    
                return  $scheme . str_replace("//", "/", $user . $pass . $host . $port . $path . $query . $fragment );
            }
    
         }
        new dm_theme_mod_mapping;
    endif;

    Please let me know if everything works fine on your end.

    Cheers,
    Jose

  • Jose

    Hey BrandCo,
    It seems that I'm missing some elements in the filter.
    I'll need to take a look on what parameters are you customizing and double check that everything works fine on your end.
    In order to do that, please send me the following information via contact form :

    - In the subject field add "Attn: Jose”
    - Link back to this thread
    - Include WP admin/network access
    - Include FTP host and credentials.

    On the contact form, select "I have a different question", this ensures it comes through and gets assigned to me.

    Cheers,
    Jose

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.