Converting CGI Movable Type Templates to PHP
I love Movable Type. I really do. But there are two things about it which really chap my hide. The first is that it doesn’t offer dynamic page serving, so I must recompile my entire site after making a change. I can live with this problem as recompiling is just a question of hitting a button and waiting awhile.
The second problem, however, is that Six Apart left a few important pages as raw CGI queries. I’m talking mainly about the Search Results page, the Comment Listing page, and the Trackback page. I understand why the company initially set things up this way since when Movable Type first came out, not nearly as many people were using PHP as they are now. But now that PHP is so widespread, it would sure be nice if the company offered its customers an easy way to convert these templates to PHP.
Short of this, I’d like to share the ways I de-CGI’d these templates on Mike Industries. If you build portions of your pages dynamically with PHP, this entry is for you.
There are three ways to eliminate CGI pages from Movable Type:
- Eliminate the Movable Type CGI templates entirely
- Use PHP to parse CGI output into a format that PHP can understand
- Create PHP pages which wrap CGI pages as includes
I ended up applying all three methods, each for a different template I was trying to deal with. We’ll start with the Comments template.
window.open is so 2003
With the release of Movable Type 3.0, I’m not sure why Six Apart didn’t get rid of the default popup commenting system. Comments in a popup window used to be okay until the advertising world completely ruined the whole concept of a popup. The fact is that today, people just don’t like popups. And with Windows XP Service Pack 2 about to be released into the population, spawning them is only going to get harder.
What a lot of Movable Type users, including myself, have done is simply place comments inline at the bottom of each post. This eliminates the popup and the resulting CGI page as well. Accomplishing this is as simple as moving the comment fields from your Comment Listing template to your Individual Archive template and changing the “Comments” link to point to your individual entry page instead of popping up the comment window.
Search is critical… don’t lose it
I’ve seen a lot of PHP-driven Movable Type blogs which have simply removed search functionality from their site, and I can only guess that it’s because the Search Results page isn’t easily skinnable out of the box. It, like the Comment Listing Page, is a .cgi page. Again, Six Apart, put your wrists out so I can slap them lightly with a ruler.
Not wanting to ditch search functionality, I scoured the web and found that one orange-haired boy from Edinburgh had figured out a way to make a CGI sub-call from a PHP page and display the results in PHP. I know CGI like the back of somebody’s hand I’ve never met, and I only know a little more about PHP, so I won’t even attempt to explain how it’s done. All I know is that it took me 5 minutes to implement, and the Orange Haired One has easy-to-follow instructions on his site. Go check it out.
Don’t lack the trackback
As with search, a lot of people seem to have removed trackback functionality from their sites as well. Some just may not like the concept of trackbacks, but if you’ve eliminated this feature just because it’s another MT CGI annoyance, it’s time to bring it back! Trackbacks are a great indicator of who else is talking about your posts at any given time.
I wasn’t able to find any information on Six Apart’s site or support forums related to converting the Trackbacks page to PHP so I then turned to Google. No dice there either so it was time to apply my 9 or 10 brain cells worth of PHP knowledge to solving this problem on my own.
To my surprise, the solution was dead-simple. So dead simple in fact that I’m thinking it might work well for search too. Through about 5 minutes of trial-and-error using various commands like implode() and virtual(), I found that by calling the trackback CGI template (mt-tb.cgi) from a PHP page using the include() command, both the CGI and the PHP will execute and the problem is solved! All of you PHP-heads out there are probably shaking your heads right now thinking I’m a moron for not knowing that already, but hey, there’s no documentation out there about it. I even did a Google search for “mt-tb.php” (a logical name to call your newly created PHP trackback page), and nothing came up.
So if you’d like to turn your Trackbacks template in a PHP page, simply create a new PHP page and put this code in it:
Then skin the original Trackback template, insert whatever PHP you want, and you’re set.
As mentioned before, I’m not a PHP expert, so if you are and you see a problem with this method (or a better way to do it), please post it in the comments. I’m frankly a little surprised that CGI executes when called as a PHP include, so if this isn’t possible on all servers, it wouldn’t shock me. Works great for me though!