# Experiments in WordPress Routing 06

Testing multisite compatibility.

## Can I Multisite?

WordPress has been offering the possibility to set up a multisite network for quite some time now and I find myself using such a feature in some client projects.
Losing this ability to an improvised and makeshift routing system would break the deal.
To test the capabilities of this solution I’ve set up a multisite network made of two sites:

1. wp.dev - the main site
2. subsite.wp.dev - the only subsite in the network

For this test I’ve taken the subdomain approach to the multisite installation to keep things “easy” in these first steps.
In the code I’ve set up just one route (/titles) that should show me the titles of the posts of the site I’m visiting.
My code is relying on WordPress built in discrimination of the site and is not making any effort to discriminate one site from the other

/**
* Plugin Name: theAverageDev Routes
* Plugin URI: http://theAverageDev.com
* Description: Routing for WordPress
* Version: 1.0
* Author: theAverageDev
* Author URI: http://theAverageDev.com
*/

/**
* Parse request
*/
function tad_routes_do_parse_request( $continue, WP$wp, $extra_query_vars ) { respond( '/titles', function () {$out   = "<h2>Site: %s</h2><h3>Post titles</h3><ul>%s</ul>";
$posts = get_posts(); echo sprintf($out, get_bloginfo( 'title' ), implode( '', array_map( function ( $post ) { return "<li>{$post->post_title}</li>";
}, $posts ) ) ); } ); dispatch_or_continue(); return$continue;
}

Visiting the address wp.dev/titles and subsite.wp.dev/titles will yield the expected titles

## Sub-directories

Switching to a sub-directory based multisite installation complicates things a bit.
The sub-directory is part of the path and I expect the unchanged code above to work on the main site, its address did not change after all, and simply not match on the subsite and yield the 404 template.
As expected the results are as follows For the code to work on the subsite too I will have to take the subfolder path into account and register an ad-hoc route.
Luckily the klein52 library packs the with methods that’s perfect for the purpose.

/**
* Plugin Name: theAverageDev Routes
* Plugin URI: http://theAverageDev.com
* Description: Routing for WordPress
* Version: 1.0
* Author: theAverageDev
* Author URI: http://theAverageDev.com
*/

function common_routes() {
respond( '/titles', function () {
$out = "<h2>Site: %s</h2><h3>Post titles</h3><ul>%s</ul>";$posts = get_posts();
echo sprintf( $out, get_bloginfo( 'title' ), implode( '', array_map( function ($post ) {
return "<li>{$post->post_title}</li>"; },$posts ) ) );
} );
}

/**
* Parse request
*/
function tad_routes_do_parse_request( $continue, WP$wp, $extra_query_vars ) { with( '/subsite', function () { common_routes(); } ); common_routes(); dispatch_or_continue(); return$continue;
}