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:

  1. Eliminate the Movable Type CGI templates entirely
  2. Use PHP to parse CGI output into a format that PHP can understand
  3. 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. 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!

35 comments on “Converting CGI Movable Type Templates to PHP”. Leave your own?
  1. Dave S. says:

    I’ll be writing up my PHP comment preview template when it’s stable; it’s about 95% there, and it appears to be working like a charm.

    My method was adapted from this write-up by Shaun Inman. It seems Shaun is quietly solving all the world’s problems while no one is looking.

  2. Mike D. says:

    Interestingly, the readfile() solution referenced in Shaun’s article does not work on my hosting provider. The CGI output gets included properly, but the PHP doesn’t fire during page draw. I’m thinking the reason for this may be that I have my PHP code in the trackback template itself, which is a CGI page. This could be bad form… I don’t know… but it seems to work great using the include() function and it lets me continue to use the built-in trackback template in MT.

    A commenter on Shaun’s thread mentioned that some ISPs may have readfile() functionality turned off as apparently it takes up some amount of memory. The virtual() command also did not work for me and the error message I got indicated that function definitely wasn’t supported. But then again, I’m no PHP expert so user error is a distinct possibility.

    The include() function was the only way I found to get the cgi and the php firing together. Any of you PHP-heads out there care to comment on the use of include() vs. virtual() vs. readfile() ? Wolf?

  3. CURL will also work, but then again it’s up to your host if you can.

  4. Dave S. says:

    “I’m thinking the reason for this may be that I have my PHP code in the trackback template itself, which is a CGI page.”

    There’s your problem. PHP doesn’t execute within a .cgi page, not even if you addtype it in your .htaccess — it has to be a .php file.

    Again, more once I get the last few kinks worked out. :)

  5. Mike D. says:

    Okay, well that makes sense then. I wonder why PHP executes just fine if you use the include() function though. Still seems like bad form though to have PHP code in a CGI file… I’ll change that pronto.

    Anybody have any insights on which is better to use from a processor standpoint, readfile(), include(), implode(), or virtual()?

  6. Geof says:

    Mike, I don’t know why you’re persisting in trying to use the wrench-like MT as a hammer when there’s a hammer that does more of what you seem to say you want in WordPress.

    Before someone accuses me of trying to suck another MT user over to WP, let me just note that I only am making this comment because Mike wrote, “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.” I grew to hate dynamic page-serving while I was a Greymatter user, and that’s why I never switched to MT.

    There are many reasons not to use WP and to use MT; I’m just throwing the idea out there.

  7. Mike D. says:


    Personally, I’m totally cool with you trying to suck people over to WordPress. I’m interested in using whatever tool is best for the job, and the only reason I used MT to begin with is that I had some experience with it, so it was easy to implement. Perhaps I should do an audit of WordPress before this blog gets too big.

    You did mention however that you didn’t switch to MT because you don’t like dynamic page-serving. MT doesn’t have dynamic page serving. It requires you to compile everything when you republish.

  8. Porter says:

    The first is that it doesn’t offer dynamic page serving…

    This is not strictly true. For some time now Movable Type’s been shipping with a script that allows you to dynamically generate pages. It’s not officially supported and it’s not terribly well documented, even unofficially, but it’s there.

  9. Glad you like the script! :)

  10. Anil says:

    As Porter mentioned, we do have a dynamic script available for people that want to go that route, but i shudder to think how much more work it would have taken to generate 50,000 pageviews with every one having to hit a database. I’m a PHP guy myself, so I know why people like dynamic stuff, but the reality is, very few people have anything that’s changing, and I’d rather wait 10 seconds to post myself than have 10,000 people each wait a second. Now, then…

    “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.” We have. We made lots of nice changes like that (such as yyyy/mm/dd/dirified_title.html post archives) but we don’t enforce them on existing site’s templates because, well, you know why. We also move comments inline on individual archives, but there’s a question about whether we should require individual archives for users who might otherwise opt not to have them.

    Personally, I’d recommend Shaun’s readfile solution, that seems most robust, at least for now. Finally, I think we’re in a position to smooth out a lot of these little niggling things that (honest to god) probably bother us even more than they bother you. Thanks for doing all the work of documenting this, though, I’m glad to pass the link on to people.

  11. Anil says:

    Err, I meant to recommend virtual, not readfile. Pity the poor suit, pretending to be a real geek.

  12. Pat says:

    Oh, then you definitely need to check out Brad Choate’s Smart Templating System. It combines MT with The PHP Smarty Template System. Wicked sick…as the kids say these days. Don’t they?

  13. Matt says:

    Whenever you include a file using a http:// path whatever the web server executes that page with — be it Perl, Python, or whatever — is run and you just get the output. It’s the same as if you included, it opens the page just like a browser would and reads the content. However this means at least one other Apache process is spawned for every page load. virtual() does the same thing. Ouch! Be nice to your web server.

    The cost of dynamic pages is highly exaggerated. I speak from experience of running a service (Ping-O-Matic) that generates well over a hundred thousand queries per day. MySQL is incredibly fast, and it gets better with every release. I also run a few other high traffic fully dynamic sites (based on WordPress) and nearly every page is generated in under a tenth of a second, most under a twentieth. Google reports a few random searches as taking 0.11-0.41 seconds. If my pages are as fast or faster than Google and cause no perceptible load on the server then I’m not worried about anything.

  14. Doug says:

    Good info Mike.

    I’ve implemented techniques very similar to these for the recent redesign of Stopdesign for search/results pages. The thing I most like (now that a majority of the site is powered by MT) is the ability to filter out or include certain sections of the site. The fact that most of the template is now driven by my own PHP and HTML means much more customization, and the ability to make the content look less like a default MT search (no offense to Jay).

    I had comment preview pages rendering in the same fashion, but comment error pages were another beast that I couldn’t figure out. Actually, it was determining whether to show an error page, or to redirect back to the entry page with the new comment added. So instead, I’ve masked the ugly URLs (my cgiwrap URLs are even longer than the standard MT paths) via mod_rewrite.

  15. sosa says:

    I’ve implemented that anti-popup technique on my blog and included comments and trackback directly upon individual entries. But once somebody post a comment the cgi was executed then redirected to somekind of limbo with no way back. My first tought was of a php-redirect but it silmply didn’t work, so i’ve made a lo-fi javascript solution that is working very well.


    where #comments is the div’s id where i had my comments

    ¿any better idea?

    sorry for my bad-english.

  16. Jay Allen says:

    Yeah, I think I got rid of all of the popup CGI crap sometime in late 2001. It’s really not difficult to do. Except for the comment error and preview pages (which must be dynamic), the rest of it goes right on your individual entry page (and I think it’s much nicer anyhow). I’m actually surprised to hear Anil say that the default templates left the popup crap in because the templates I’m using (which are leftover from the beta test) completely eradicated them…

    Anyway, I’ve been playing around a bit with mt-view.cgi but I agree with Anil, it makes no sense to render four years of content when essentially less than 10-20 pages on your site are changing at all.

    And search? Well, you can blame that one on me. The first version of MT-Search was written in late 2001 when Movable Type hadn’t even matured to 1.2. In short, it got included a good while later into v2.5 and really hasn’t changed too terribly much since then, mainly because it works and there aren’t a whole lot of other ways to do it unless you’re going to take the step of creating search indexes and cached queries.

    To be honest, I don’t blame Six Apart at all for just leaving it as is. They had far bigger fish to fry and perfectionism is a good way to go out of business… Or in Ben and Mena’s case at the time, to avoid going INTO business…

  17. Quadsk8 says:

    Sorry, but your anchor name “entercomment” above the comment form is missing, so it will not be linked form your posted-by-line

  18. Mike D. says:

    Quadsk8: Thanks for pointing that out. It is now fixed.

    Jay: I agree that getting rid of the popups and cgi crap is not that hard, if you know what you’re doing. But my point is that if Movable Type is trying to be a mainstream product that a greater cross-section of people will use, Six Apart should provide this sort of configurability out of the box. I think that these days, there are a lot more “light developers” out there who feel comfortable messing with a little PHP than with CGI. Sure, if you’re determined enough, you can figure it out. But if I could took a day or two and get this done, why shouldn’t Six Apart take a day or two and just get it done for everybody in one shot?

    I’m amazed at how many people linked to this article with comments like “I’ve been wanting to do this for a long time…”

    You may be right about Six Apart having bigger fish to fry, but I just don’t see a ton of huge features in 3.0 so far, so I’d at least just expect it to be easier to use and customize.

  19. Mike D. says:


    Thanks for the post. I could be crazy, but I swear that when I installed 3.0D, it used comment popups. Did I download an early version or did I just miss a preference or something?

  20. fluffy says:

    Comment popups are even more harmful than being “ruined by advertising.” It detaches the comments from the weblog entry being read, and it makes it impossible to actually load/read/comment on multiple entries in parallel.

    Personally, I just went and replaced the entire comment engine, but that’s just me.

  21. bruce says:

    On our blog of over 4000 entries I use the include(comments.cgi?id=x) function for each individual archive page. Might be a slight extra load on the server when the pages are served, but keeps the disk size right down (would be impossible to have individual archives otherwise) and saves having to do any rebuilding if I want to make any changes to the individual archive template, becuase it’s using the comments cgi.

  22. Rolf says:

    I converted that times and functioned it with my side well…
    geat work – thanks

  23. Mike says:

    Hey Mike, I had your site bookmarked for a while because I was trying to implement the CGI –> PHP crossover technique in Movable Type. I eventually gave up because skinning the search page was so frustrating even though the Red Haired One explained setting up the mechanism very well (I’m not really a code guy, I just started building sites a month ago). Anyway, the frustration of the CGI Comments, Trackbacks, and Search and this entry chain caused me to move over from Movable Type to WordPress.

    It’s simply beautiful how it works and if it doesn’t work, someone out there will help you with it. It’s less clunky, cleaner, and less expensive ($0). The only advantage I see MT having over WordPress is multiple blogs, but that’s a minor advantage considering how easy it is to install another WordPress to act as another blog. However, the static pages (the reason why you rebuild in MT, for the impatient MT users out there) do make the site faster for the end-user of an MT blog and slower for the end-user of a WP blog.

    Now I’m not trying to convert anyone to WordPress. I just have one question. Is there any way to have a high-volume website using PHP without users getting slowed down? I think I saw a plugin for WordPress that creates static pages, but there must be some way to avoid doing this. Is it too much to hope for?

  24. Will says:

    Just thought I’d include some links 6A was nice enough to send me:

    (the others, you have)

    One I found on my own:

    — I tried the shauninman thing using include(); my isp didn’t like the others. For some reason I can only get it to show using an http: call, not a server path. -weird.

    Oh well; Cheers!


  25. Suchi Garg says:

    I am using MT – and was getting nice urls to searches by doing a web include of the cgi script in php files. Something like
    include “”;

    Now this is causing certain issues with the web server processing – and i need to get rid of this. I have looked at the several links offered – and tried most of them. Do you by any chance have a copy of what the “orange haired boy” said abt this. His site is now closed – and hence i am not able to read his article.

    Thank you

  26. MT PHP Trickery

    Two related articles here on converting CGI Moveable Type templates to PHP, for easier manipulation (for those of us comfortable with PHP at least). First up is Mike Davidson’s article and an article that influenced this, over at orange haired…

  27. fine-tuning mt output with php

    i just fixed a little problem i encountered with my design. problem is or now was, that the column to the far left is too narrow to have longer entry titles in the “recently” section on one line. if the…

  28. Converting CGI Movable Type Templates to PHP

    comments, search and trackback tips…

  29. Blog-Fu says:

    CGI MT Templates to PHP

    Mike Davidson is feed up with a couple of aspects of the templating system, and raw CGI queries of MovableType. So he’s provided us a good article on Converting CGI Movable Type Templates to PHP. He outlines three methods to…


    this is getting crazy. im setting up a linklog. converting cgi movable type templates to php [via] wow, what a…

  31. links for 2004-12-05

    Webmonkey | Reference: HTML Cheatsheet I used this when I first started out in HTML. [via Linkfilter] (categories: HTML…

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe by Email

... or use RSS