Skip to content

Instantly share code, notes, and snippets.

@kovshenin
Created November 14, 2012 12:51
Show Gist options
  • Save kovshenin/4071933 to your computer and use it in GitHub Desktop.
Save kovshenin/4071933 to your computer and use it in GitHub Desktop.
<?php
/**
* In theory should support the following subdir sites as port of a subdomain network:
* - subdomain.example.org/site1
* - subdomain.example.org/site2
*/
function can_i_haz_subdomain_and_subdir() {
global $wpdb, $current_blog, $current_site, $blog_id, $site_id;
// Taken from ms-settings.php
$domain = $_SERVER['HTTP_HOST'];
$path = preg_replace( '|([a-z0-9-]+.php.*)|', '', $_SERVER['REQUEST_URI'] );
$path = str_replace ( '/wp-admin/', '/', $path );
$path = preg_replace( '|(/[a-z0-9-]+?/).*|', '$1', $path );
$path = rtrim( $path, '/' ) . '/'; // trailingslashit
// Currently supports one subdomain only
if ( $domain != 'something.example.org' )
return;
$blog = get_blog_details( array( 'domain' => $domain, 'path' => $path ), false );
if ( ! $blog )
return;
// Make WordPress think this is the right blog/site pair.
$current_blog = $blog;
$blog_id = $current_blog->blog_id;
$site_id = $current_blog->site_id;
// See ms-settings.php and mu domain mapping plugin.
$current_site = $wpdb->get_row( $wpdb->prepare( "SELECT * from $wpdb->site WHERE id = %d LIMIT 1", $current_blog->site_id ) );
$current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s LIMIT 1", $current_site->domain, $current_site->path ) );
$current_site = get_current_site_name( $current_site );
}
can_i_haz_subdomain_and_subdir();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment