Success with the Movable Type Comment Templates redo
So I’ve been trying to find a reasonable work around for the fact that you can’t include PHP code in any of the Movable Type System templates or include modules in the System Templates that contain PHP. I tried a couple different solutions with no luck but riding home on the bus one day came up with a brainstorm. What if I just made the comment system templates redirect to an appropriate page (a regular page) that looked the way I wanted it to. Sounds simple right? Well, it took a little more than I initially anticipated but I got it to work satisfactorily. I won’t say it is perfect but its better than having my PHP code show on the page or having the pages not have a side navigation bar because it relies on PHP.
Here is what I did. I changed the Comment Pending form to:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" id="sixapart-standard">
<head>
<META HTTP-EQUIV="Refresh" CONTENT="0; URL=<$MTBlogURL$>comment-pending.html?entry_id=<$MTEntryID$>">
<title><$MTBlogName encode_html="1"$>: Comment Pending</title>
</head>
<body>
</body>
</html>
The I created a new template called Comment Pending which outputs the file comment-pending.html. It contains the following code.
<?php
// Get the form fields.
$entry_id = $_REQUEST['entry_id'];
$author = urldecode($_REQUEST['author']);
$email = urldecode($_REQUEST['email']);
$url = $_REQUEST['url'];
$text = urldecode($_REQUEST['text']);
$date = date(' F d Y h:i:s A');
global $mt, $db;
$db = $mt->db;
$entry = $db->get_row("SELECT * from mt_entry where entry_id = $entry_id");
$entry_base_name = $entry->entry_basename;
$entry_created_on = $entry->entry_created_on;
$year = substr($entry_created_on, 0, 4);
$month = substr($entry_created_on, 5, 2);
$permalink = $year . "/" . $month . "/" . $entry_base_name . ".html";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" id="sixapart-standard">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<$MTPublishCharset$>" />
<meta name="generator" content="Movable Type <$MTVersion$>" />
<link rel="stylesheet" href="<$MTBlogURL$>styles-site.css" type="text/css" />
<title><$MTBlogName encode_html="1"$>: Comment Pending</title>
</head>
<body class="layout-two-column-right">
<div id="container">
<div id="container-inner" class="pkg">
<$MTInclude module="banner module"$>
<div id="pagebody">
<div id="pagebody-inner" class="pkg">
<div id="alpha">
<div id="alpha-inner" class="pkg">
<h3>Thank you for commenting.</h3>
<p>Your comment has been received and held for approval by the blog owner.</p>
<p><a href="<$MTBlogURL$><?php echo $permalink; ?>">Return to the original entry</a></p>
</div>
</div>
<$MTInclude module="sidebar module" $>
</div>
</div>
</div>
</div>
</body>
</html>
I couldn’t just cut and paste the code that was in the originally in the system comment pending template because when I do the redirect, somehow Moveable Type loses track of the entry I’m dealing with and the xxx tag doesn’t work correctly. So I wrote a PHP subsitute for this to generate the link instead.
The Comment Preview form is handled the same way. I change the system comment preview form template to the following code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" id="sixapart-standard">
<head>
<META HTTP-EQUIV="Refresh" CONTENT="0; URL=<$MTBlogURL$>comment-preview.html?entry_id=<$MTEntryID$>&author=<$MTCommentPreviewAuthor$>&email= <$MTCommentPreviewEmail$>&url=<$MTCommentPreviewURL$>&text=<$MTCommentPreviewBody autolink="0" sanitize="0" convert_breaks="0"$>">
<title><$MTBlogName encode_html="1"$>: Comment Preview</title>
</head>
<body>
</body>
</html>
and then create a new template called Comment Preview (outfile: comment-preview.html) which has this code.
<?php
$entry_id = $_REQUEST['entry_id'];
$author = urldecode($_REQUEST['author']);
$email = urldecode($_REQUEST['email']);
$url = $_REQUEST['url'];
$text = urldecode($_REQUEST['text']);
$error = $_REQUEST['error'];
$date = date(' F d Y h:i:s A');
$BlogID = $this->tag('MTBlogID');
global $mt, $db;
$db = $mt->db;
$entry = $db->get_row("SELECT * from mt_entry where entry_id = $entry_id");
$entry_allow_comments = $entry->entry_allow_comments;
$blog = $db->get_row("SELECT * from mt_blog where blog_id =$BlogID");
$blog_allow_comment_html = $blog->blog_allow_comment_html;
$blog_allow_reg_comments = $blog->blog_allow_reg_comments;
$blog_allow_unreg_comments = $blog->blog_allow_unreg_comments;
$blog_moderate_unreg_comments = $blog->blog_moderate_unreg_comments;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" id="sixapart-standard">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<$MTPublishCharset$>" />
<meta name="generator" content="Movable Type <$MTVersion$>" />
<link rel="stylesheet" href="<$MTBlogURL$>styles-site.css" type="text/css" />
<title><$MTBlogName encode_html="1"$>: Comment on <$MTEntryTitle$></title>
<script type="text/javascript" src="<$MTBlogURL$>mt-site.js"></script>
<script type="text/javascript" src="<MTStaticWebPath>js/commenter_name.js"></script>
</head>
<body class="layout-two-column" onload="individualArchivesOnLoad(commenter_name)">
<div id="container">
<div id="container-inner" class="pkg">
<$MTInclude module="banner module"$>
<div id="pagebody">
<div id="pagebody-inner" class="pkg">
<div id="alpha">
<div id="alpha-inner" class="pkg">
<?php
if (!($error == "")) {
echo "<h3>Comment Submission Error</h3>";
echo "<p>Your comment submission failed for the following reasons:</p>";
echo $error;
} else {
?>
<h3>Previewing your Comment</h3>
<div class="comment">
<div class="comment-content">
<?php echo $text;?>
</div>
<p class="comment-footer">
Posted by:
<?php
if ($author == "") {
echo "Anonymous";
}else{
echo $author;
}
?>
| <?php echo $date;?>
</p>
</div>
<?php
}
if ($entry_allow_comments == 1) {
?>
<form method="post" action="<$MTCGIPath$><$MTCommentScript$>" name="comments_form" onsubmit="if (this.bakecookie.checked) rememberMe(this)">
<input type="hidden" name="static" value="1" />
<input type="hidden" name="entry_id" value="<?php echo $entry_id; ?>" />
<div class="comments-open" id="comments-open">
<div class="comments-open-content">
<?php
if ($blog_moderate_unreg_comments == 1) {
?>
<p class="comments-open-moderated">
(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)
</p>
<?php
}
?>
<div id="comments-open-data">
<div id="name-email">
<?php
if ( $blog_allow_unreg_comments==0) {
} else {
?>
<p>
<label for="comment-author">Name:</label>
<input id="comment-author" name="author" size="30" value="<?php echo $author; ?>" />
</p>
<p>
<label for="comment-email">Email Address:</label>
<input id="comment-email" name="email" size="30" value="<?php echo $email; ?>" />
</p>
<?php
}
?>
</div>
<p>
<label for="comment-url">URL:</label>
<input id="comment-url" name="url" size="30" value="<?php echo $url; ?>" />
</p>
</div>
<p id="comments-open-text">
<label for="comment-text">Comments:
<?php
if ($blog_allow_comment_html == 1) {
echo "(you may use HTML tags for style)";
}
?>
</label>
<textarea id="comment-text" name="text" rows="10" cols="30">
<?php echo $text; ?>
</textarea>
</p>
<div id="comments-open-footer" class="comments-open-footer">
<input type="submit" name="preview" id="comment-preview" accesskey="v" value="Preview" />
<input type="submit" name="post" id="comment-post" accesskey="s" value="Post" />
<input type="button" name="cancel" id="comment-cancel" value="Cancel" onclick="window.location='<$MTEntryPermalink$>'" />
</div>
</div>
</div>
</form>
<?php
}
?>
<MTIfCommentsActive>
<a id="comments"></a>
<div class="comments">
<div class="comments-content">
<MTComments>
<MTCommentsHeader>
<h3 class="comments-header">Comments</h3>
</MTCommentsHeader>
<a id="c<$MTCommentID pad="1"$>"></a>
<div class="comment" id="comment-<$MTCommentID$>">
<div class="comment-content">
<$MTCommentBody$>
</div>
<p class="comment-footer">
Posted by:
<$MTCommentAuthorLink default_name="Anonymous"$> <MTCommentAuthorIdentity> |
<a href="#comment-<$MTCommentID$>"><$MTCommentDate$></a>
</p>
</div>
</MTComments>
</div>
</div>
</MTIfCommentsActive>
</div>
</div>
<$MTInclude module="sidebar module" $>
</div>
</div>
</div>
</div>
</body>
</html>
Once again I had to change many of the original Movable Type tags to PHP which does the same thing so that I can get it to work. However, the end result uses the same templates as the rest of my site and allows me to keep everything looking the uniform with the same code.
I also tweaked the system Comment Error template so that it redirects to comment-preview.html as well. comment-preview.html is setup to receive the error message from the Comment Error template and display it. If there isn’t an error message then it displays like the normal comment preview form. Here is the code I put in my system Comment Error template.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" id="sixapart-standard">
<head>
<META HTTP-EQUIV="Refresh" CONTENT="0; URL=<$MTBlogURL$>comment-preview.html?entry_id=<$MTEntryID$>&author=<$MTCommentPreviewAuthor$>&emai= <$MTCommentPreviewEmail$>&url=<$MTCommentPreviewURL$>&text=<$MTCommentPreviewBody autolink="0" sanitize="0" convert_breaks="0"$>&error=<$MTErrorMessage$>">
<title><$MTBlogName encode_html="1"$>: Comment Error</title>
</head>
<body>
</body>
</html>
It took quite a while to work this out. However, I’m pretty happy with the outcome. Maybe Movable Type will decide in the next version that they should redesign the comment and search functions so that users can consistently use PHP in their templates. Until then this solution works for us.