Multisite – sub-subfolders for certain blogs

In order to do partition your blogs like this you’re going to need to write a custom plugin similar to the WordPress MU Domain Mapping plugin. Here’s how your plugin needs to work.

  1. Create a sunrise.php file for your plugin, and properly define('SUNRISE',true); in your wp-config.php file.
  2. Create a table which maps the tuples {blog_slug, blog_id, is_archived}

In sunrise.php perform the following logic:

$access_url = $_SERVER[ 'REQUEST_URI' ];
$on_archive = false;
$slug = "";
$parts = explode("https://wordpress.stackexchange.com/",$access_url);
for($i = 0; $i < count($parts); ++$i) {
   if(strpos($parts[i],"yourdomain.com") !== false) {
      if($parts[i + 1] == "archived") {
         $on_archive = true;
         $slug = $parts[i+2]; // TODO: Index checking
      } else
         $slug = $parts[i + 1];
      break;
   }
}

$blog_id = $wpdb->get_var("SELECT blog_id FROM YOURTABLE WHERE blog_name=$slug ...");
if($blog_id && $on_archive) {
   $current_blog = $wpdb->get_row("SELECT * FROM {$wpdb->blogs} WHERE blog_id = '$blog_id' LIMIT 1");
   $current_blog->path="/archived";
   $site_id = $current_blog->site_id;

   $current_site = $wpdb->get_row( "SELECT * from {$wpdb->site} WHERE id = '{$current_blog->site_id}' LIMIT 0,1" );
   $current_site->blog_id = $wpdb->get_var( "SELECT blog_id FROM {$wpdb->blogs} WHERE domain='{$current_site->domain}' AND path="{$current_site->path}"" );
} else {
   // Things went wrong, redirect to the home page, or something
}

Finally, populate the fields of your table so that it knows which blogs are “archived”. In production you’d probably want to add some array safety checks, as well as checking that if “blog_a” is archived, then visiting yourdomain.com/blog_a redirects to yourdomain.com/archived/blog_a. All of these things can be handled in the sunrise file.

Leave a Comment