How to make the Sitemap Plugin work for Wordpress mu

The next logical step after getting wordpress mu to work with multiple domains is to tell google all about your sites via google compliant sitemap.xml files. If you’re anything like me, you would have searched through the mu forums and come out more confused than what you started with. There’s about three attempts to make a mu plugin over at wpmudev.org and they all fail dismally. This is so not typical of wordpress as normally, there’s a plugin developed for just about anything you can think of. With such a gap in the mu works, I’ve had to hack my own sitemap plugin to get my domain parking mu working. Luckily, Arne Brachhold’s plugin for the normal wordpress is very nicely coded and has all the bells and whistles that you’d ever what a sitemap plugin to have.

The first problem with Arne’s plugin is figuring out where to put the sitemap.xml since you have so many blogs running off the one wordpress. If you put it in your root directory, it will be overwritten by every blog as they are all pointing to the same location. The first hack is to default the location of the output to each blog’s individual upload folder (ie. /wp-content/blogs.dir//files/) and to create it if it doesn’t exist already (wordpress only creates the upload folders the first time you try to upload something)

Edit the InitOptions() function in the sitemap.php file


//JH brute force - set the sitemap location to manual mode
$this->_options["sm_b_location_mode"]="manual"; //Mode of location, auto or manual
//workout what the upload directory for this blog is
$upload_dir = ABSPATH . UPLOADS;
//create the directory if it does not already exist
wp_mkdir_p($upload_dir);
//preset the manual directory and url
$this->_options["sm_b_filename_manual"]= $upload_dir . "sitemap.xml";
$this->_options["sm_b_fileurl_manual"]= trailingslashit(get_bloginfo('siteurl')) . "sitemap.xml";

Next we want to force it to output a robots.txt file every time so that the search engine spiders know where to find the sitemap. To do this we set the robots flag to true in the same initOptions function.
$this->_options["sm_b_robots"] = true;

There is also a bug in the GetRobotsFilePath() function in that it is not putting it in the same location as the sitemap path. Another dirty hack is needed:

function GetRobotsFilePath() {
//return trailingslashit($this->GetHomePath()) . 'robots.txt';
//JH brute force
return ABSPATH . UPLOADS . "robots.txt";
}

That’s it for the sitemap.php file.

The next hack is to wordpress itself. Wordpress mu is only up to v2.3.1 of wordpress and there is a bug in one of the functions to write to a file. Whilst we are waiting for mu to update, this silly line needs to be commented out from one of the functions.

function insert_with_markers( $filename, $marker, $insertion ) {
//return;

Yep, that’s exactly right. Someone’s put a return statement as the first line of code so that this function exits straight away without actually creating and editing the file. This function is used to create your .htaccess file and the sitemap plugin reuses it to write your robots.txt

We’re almost there. Google doesn’t like your sitemap.xml file to be anywhere except your root directory. It is currently sitting in http://yourblog.com/files/sitemap.xml and we want to actually trick google to see it as http://www.yourblog.com/sitemap.xml. To do this, we need to edit the .htaccess file on your root directory:

#uploaded files
RewriteRule ^(.*/)?files/$ index.php [L]
RewriteRule ^(.*/)?files/(.*) wp-content/blogs.php?file=$2 [L]
#sitemap
RewriteRule ^sitemap.xml$ wp-content/blogs.php?file=sitemap.xml [L]
RewriteRule ^sitemap.xml.gz$ wp-content/blogs.php?file=sitemap.xml.gz [L]
RewriteRule ^robots.txt$ wp-content/blogs.php?file=robots.txt [L]

Those three lines under #sitemap will redirect calls to the files to your blogs upload directory. The blogs.php file is mu’s handler to redirect to the right blog directory depending on what domain you are accessing.

Phew that’s it! Hope I didn’t lose anyone. It could have been a bit cleaner but it’s a hack specifically for my wordpress mu so it doesn’t need to be pretty :) All that there is to do now is to login to your wordpress, enable plugins for your blog, activate the sitemap plugin and generate your first sitemap.

O. And when you submit your sitemap to google’s webmaster tools, you should use the meta tag option to verify your site rather than the file option. This avoids having hundreds of google2143293udsf0.html files in your root directory. An easy way to do this is the Add Meta Tag plugin. Remember, both the add Meta Tag plugin and the Sitemap plugin go in the wp-content/plugins directory as they are just normal wordpress plugins.

Update 26/6/2008: I’ve finally gotten some free time to clean up this hack and upload it so that you don’t have to edit the code anymore. Download it here on my new bitminds software site.

Popularity: 20%