dmtable = $wpdb->base_prefix . 'domain_mapping'; if ( VHOST == 'no' ) { die( 'Sorry, domain mapping only works on virtual host installs.' ); } if ( is_site_admin() ) { if($wpdb->get_var("SHOW TABLES LIKE '{$wpdb->dmtable}'") != $wpdb->dmtable) { $wpdb->query( "CREATE TABLE IF NOT EXISTS `{$wpdb->dmtable}` ( `id` bigint(20) NOT NULL auto_increment, `blog_id` bigint(20) NOT NULL, `domain` varchar(255) NOT NULL, `active` tinyint(4) default '1', PRIMARY KEY (`id`), KEY `blog_id` (`blog_id`,`domain`,`active`) );" ); ?>
" . __( "As a site admin on this site you can set the IP address users need to point their DNS A records at. If you don't know what it is, ping this blog to get the IP address." ) . "
"; echo "" . __( "If you use round robin DNS or another load balancing technique with more than one IP, enter each address, separating them by commas." ) . "
"; echo '" . __( 'If your domain name includes a hostname like "blog" or some other prefix before the actual domain name you will need to add a CNAME for that hostname in your DNS pointing at this blog URL. "www" does not count because it will be removed from the domain name.' ) . "
"; $dm_ipaddress = get_site_option( 'dm_ipaddress', 'IP not set by admin yet.' ); if ( strpos( $dm_ipaddress, ',' ) ) { echo "" . __( 'If you want to redirect a domain you will need to add DNS "A" records pointing at the IP addresses of this server: ' ) . "" . $dm_ipaddress . "
"; } else { echo "" . __( 'If you want to redirect a domain you will need to add a DNS "A" record pointing at the IP address of this server: ' ) . "" . $dm_ipaddress . "
"; } echo ""; } function domain_mapping_siteurl( $setting ) { global $wpdb, $current_blog; // To reduce the number of database queries, save the results the first time we encounter each blog ID. static $return_url = array(); if ( !isset( $return_url[ $wpdb->blogid ] ) ) { $s = $wpdb->suppress_errors(); // Try matching on the current URL domain and blog first. This will take priorty. $domain = $wpdb->get_var( $wpdb->prepare( "SELECT domain FROM {$wpdb->dmtable} WHERE domain = %s AND blog_id = %d LIMIT 1", preg_replace( "/^www\./", "", $_SERVER[ 'HTTP_HOST' ] ), $wpdb->blogid ) ); // If no match, then try against the blog ID alone (which we get, without a 'preferred domain' setting, // will be a matter of luck. if ( empty( $domain ) ) { $domain = $wpdb->get_var( $wpdb->prepare( "SELECT domain FROM {$wpdb->dmtable} WHERE blog_id = %d", $wpdb->blogid ) ); } $wpdb->suppress_errors( $s ); $protocol = ( 'on' == strtolower( $_SERVER['HTTPS' ] ) ) ? 'https://' : 'http://'; if ( $domain ) { $return_url[ $wpdb->blogid ] = untrailingslashit( $protocol . $domain . $current_blog->path ); $setting = $return_url[ $wpdb->blogid ]; } else { $return_url[ $wpdb->blogid ] = false; } } elseif ( $return_url[ $wpdb->blogid ] !== FALSE) { $setting = $return_url[ $wpdb->blogid ]; } return $setting; } function domain_mapping_post_content( $post_content ) { global $wpdb; static $orig_urls = array(); if ( ! isset( $orig_urls[ $wpdb->blog_id ] ) ) { remove_filter( 'pre_option_siteurl', 'domain_mapping_siteurl' ); $orig_url = get_option( 'siteurl' ); $orig_urls[ $wpdb->blog_id ] = $orig_url; add_filter( 'pre_option_siteurl', 'domain_mapping_siteurl' ); } else { $orig_url = $orig_urls[ $wpdb->blog_id ]; } $url = domain_mapping_siteurl( 'NA' ); if ( $url == 'NA' ) return $url; return str_replace( $orig_url, $url, $post_content ); } if ( defined( 'DOMAIN_MAPPING' ) ) { add_filter( 'pre_option_siteurl', 'domain_mapping_siteurl' ); add_filter( 'pre_option_home', 'domain_mapping_siteurl' ); add_filter( 'the_content', 'domain_mapping_post_content' ); } function redirect_to_mapped_domain() { global $current_blog; $protocol = ( 'on' == strtolower($_SERVER['HTTPS']) ) ? 'https://' : 'http://'; $url = domain_mapping_siteurl( false ); if ( $url && $url != untrailingslashit( $protocol . $current_blog->domain . $current_blog->path ) ) { wp_redirect( $url . $_SERVER[ 'REQUEST_URI' ] ); exit; } } add_action( 'template_redirect', 'redirect_to_mapped_domain' ); // delete mapping if blog is deleted function delete_blog_domain_mapping( $blog_id, $drop ) { global $wpdb; $wpdb->dmtable = $wpdb->base_prefix . 'domain_mapping'; if ( $blog_id && $drop ) { $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->dmtable} WHERE blog_id = %d", $blog_id ) ); } } add_action( 'delete_blog', 'delete_blog_domain_mapping', 1, 2 ); // show mapping on site admin blogs screen function ra_domain_mapping_columns( $columns ) { $columns[ 'map' ] = __( 'Mapping' ); return $columns; } add_filter( 'wpmu_blogs_columns', 'ra_domain_mapping_columns' ); function ra_domain_mapping_field( $column, $blog_id ) { global $wpdb; static $maps = false; if ( $column == 'map' ) { if ( $maps === false ) { $wpdb->dmtable = $wpdb->base_prefix . 'domain_mapping'; $work = $wpdb->get_results( "SELECT blog_id, domain FROM {$wpdb->dmtable} ORDER BY blog_id" ); $maps = array(); if($work) { foreach( $work as $blog ) { $maps[ $blog->blog_id ] = $blog->domain; } } } echo $maps[ $blog_id ]; } } add_action( 'manage_blogs_custom_column', 'ra_domain_mapping_field', 1, 3 ); ?>