SEO friendly WordPress Network domain mapping URL filters

When running a WordPress network with domain mapping, some clients and visitors find it less than re-assuring when the network’s domain appears in the page content (as opposed to the pretty mapped domain).

The domain mapping plugin WordPress MU Domain Mapping hasn’t been updated for a while, but it still works perfectly well (WordPress 3.4.2). It filters most content extremely well. However, with the constant advance of WordPress core, a few things are slipping the net.

Headers and Background

/**
 * Fix header image url
 */
add_filter( 'theme_mod_background_image', 'tcb_domap_url_fix' );
add_filter( 'theme_mod_header_image',     'tcb_domap_url_fix' );
function tcb_domap_url_fix( $content ){
  if( is_multisite() ) :
    if( function_exists('domain_mapping_post_content') ) :
      $content = domain_mapping_post_content( $content );
    endif;
  endif;
  return $content;
}

I don’t know if it ever did these (I’ve not been using it long enough to know), but currently these are missed. Easily fixed with the above snippet.

Image Widget Plugin

Some of our sites use the Image Widget plugin. The author has kindly added some filters so we can fix it in exactly the same way.

/**
 * Fix image widget.
 */
add_filter( 'image_widget_image_url',  'tcb_domap_image_widget_url_link_mapping', 1, 3 );
add_filter( 'image_widget_image_link', 'tcb_domap_image_widget_url_link_mapping', 1, 3 );
function tcb_domap_image_widget_url_link_mapping( $content, $args, $instance ){
  if( is_multisite() ) :
    if( function_exists('domain_mapping_post_content') ) :
      $content = domain_mapping_post_content( $content );
    endif;
  endif;
  return $content;
}

May all your URLs be pretty!