How to fix WordPress MultiSite switch_to_blog() permalinks

August 11th, 2010  |  Published in Mind

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 :).

Tags: , , , , , ,

Call Event Calendar 3 in WPMS from a different site ID

July 29th, 2010  |  Published in Mind

I’ve been working on a pretty complicated WordPress Multi-Site project for the College of Education at UGA. One of their sites will be controlling the master calendar for the main website. We wanted to display the calendar from the sub-site. One the main site of the page (an possibly on other sub-sites).

Generally speaking, in WPMS / WPMU you can get the content of one site by switching to that site ID using the switch_to_blog($id) function. However, this does not work with Event Calendar 3, because of the way it builds it’s global $ec3 variable.

The problem is, when you are on site id 1, the $ec3 creates a variable that points to the schedule table for site id 1. Even if you run switch_to_blog(2), $ec3 still tries to get the schedule from site id 1. So it was returning an empty calendar. The easiest way to fix this (until EC3 updates their code) was to re-instantiate the global $ec3 variable. Basically this is what I did:

switch_to_blog(10);
global $ec3;
$ec3 = new ec3_Options();
ec3_get_calendar();
restore_current_blog();

This re-instantiates the $ec3 variable for the schedule I needed to get from that specific site ID. It should be a fairly simple fix for EC3. Until then, this is the easiest fix if you need to do what I’m doing.

Tags: , , ,