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`) );" ); ?>

escape( preg_replace( "/^www\./", "", $_POST[ 'domain' ] ) ); check_admin_referer( 'domain_mapping' ); switch( $_POST[ 'action' ] ) { case "add": if( null == $wpdb->get_row( "SELECT blog_id FROM {$wpdb->dmtable} WHERE domain = '$domain'" ) ) $wpdb->query( "INSERT INTO {$wpdb->dmtable} ( `id` , `blog_id` , `domain` , `active` ) VALUES ( NULL, '" . intval( $wpdb->blogid ) . "', '$domain', '1')" ); break; case "delete": $wpdb->query( "DELETE FROM {$wpdb->dmtable} WHERE domain = '$domain'" ); break; case "ipaddress": if( is_site_admin() ) add_site_option( 'dm_ipaddress', $_POST[ 'ipaddress' ] ); break; } } echo "

Domain Mapping

"; if( !file_exists( ABSPATH . '/wp-content/sunrise.php' ) ) { echo "Please copy sunrise.php to " . ABSPATH . "/wp-content/sunrise.php and uncomment the SUNRISE definition in " . ABSPATH . "wp-config.php"; echo "
"; die(); } if( !defined( 'SUNRISE' ) ) { echo "Please uncomment the line //define( 'SUNRISE', 'on' ); in your " . ABSPATH . "/wp-config.php"; echo ""; die(); } if( is_site_admin() ) { echo '

' . __( 'Site Admin Configuration' ) . '

'; echo "

" . __( "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 '
'; echo ''; _e( "Server IP Address: " ); echo ""; wp_nonce_field( 'domain_mapping' ); echo ""; echo "

"; } $domains = $wpdb->get_results( "SELECT * FROM {$wpdb->dmtable} WHERE blog_id = '{$wpdb->blogid}'" ); if( is_array( $domains ) && !empty( $domains ) ) { ?>

'; echo $details->domain . " "; echo ''; echo ""; echo ""; wp_nonce_field( 'domain_mapping' ); echo "
"; } ?>
" . __( 'Add new domain' ) . ""; echo '
'; echo ''; echo "http://www./"; wp_nonce_field( 'domain_mapping' ); echo ""; echo "

"; 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; $s = $wpdb->suppress_errors(); $domain = $wpdb->get_var( "SELECT domain FROM {$wpdb->dmtable} WHERE blog_id = '{$wpdb->blogid}'" ); $wpdb->suppress_errors( $s ); $protocol = ( 'on' == strtolower($_SERVER['HTTPS']) ) ? 'https://' : 'http://'; if( $domain ) return untrailingslashit( $protocol . $domain . $current_blog->path ); return $setting; } add_action( 'pre_option_siteurl', 'domain_mapping_siteurl' ); add_action( 'pre_option_home', 'domain_mapping_siteurl' ); function redirect_admin_page() { global $current_blog; $url = domain_mapping_siteurl( 'url' ); $protocol = ( 'on' == strtolower($_SERVER['HTTPS']) ) ? 'https://' : 'http://'; if( strpos( $protocol . $_SERVER[ 'HTTP_HOST' ], $url ) === false ) { wp_safe_redirect( $url . $_SERVER[ 'REQUEST_URI' ] ); } } add_action( 'admin_init', 'redirect_admin_page' ); 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' ); ?>