How to fix WordPress MultiSite switch_to_blog() permalinks

If you are using switch_to_blog() in your WordPress MultiSite installation and notice that your permalinks contain “/blog/” in them (or some other oddity). The give John James Jacoby’s plugin Switch Site Rewrite a go.

Today I ran into this problem with a WPMS site that I am developing. I needed to pull posts from the “news” site onto the main site. I used switch_to_blog() to do this, but when I tried to get specific category permalinks with get_category_links() it would automatically include “/blog/” in the URL.

This is because of the way that the $wp_rewrite variable is initialized and an attempt to reduce overhead when using switch_to_blog(). So, John’s plugin does come with a risk of added overhead, but I’m currently dealing with a 30 site installation and don’t see much of  a cost. If you were really picky, you could modify his code a bit to only work in real specific situations.

If you get  a chance, jump on over to twitter and thank him for this little plugin. It saved me a little more development work, although it also meant I wasted 2 hours tracking down the problem :).