pMachine: Customizing the comment form when registration is required

Posted by Pierre Igot in: Blogging
February 3rd, 2005 • 5:18 am

As regular Betalogue readers know, due to comment spam issues, I was recently forced to change the default behaviour on my blog for users wanting to post comments on a blog item. Betalogue readers now have to register before they can post a comment.

Before that, unregistered users could see a form at the bottom of each blog item with fields for their name, e-mail address, home page URL, and comment text. They could fill out these fields and post a comment that way, without having to register with the site. Of course, since there was no proper registration process, there was no way to force people to submit a valid e-mail address, and it was too easy for comment spammers to use the form to post comment spam.

So I changed the settings in pMachine (my blog software) to require registration.

This eliminated the above mentioned form. But the problem is that this setting change caused pMachine to simply use the comment form for registered users by default — regardless of whether the person currently reading the blog was actually registered (and logged in) or not.

If you visited a page on my blog and wanted to add a comment, you would go to the “Comments” section at the bottom and see a simple form with the line “Add your own comment:” and a text field for the comment text.

An unsuspecting user would just assume that all he had to do was to type his comment and click on the “Submit” button.

Unfortunately, for unregistered users, this would simply take them to a page instructing them to register and log in first.

This was clearly inappropriate in terms of user friendliness. Yet it is, rather inexplicably, the default behaviour in pMachine when you check the option to always require registration for users who want to post a comment.

Recently, a reader quite rightly pointed out to me that this was not really acceptable. So I decided to take a closer look and see if I could improve the interface for unregistered users.

I was quickly able to determine that the pMachine user interface itself offers no option for customizing the user interface for the comment form. There are only two forms, one for registered users and one for unregistered users — and, as soon as you check the option to require registration, pMachine always uses the comment form for registered users, regardless of whether the current user is actually registered or not.

Fortunately, through my recent experiences with setting up a web site for my employer that is driven by the open source WordPress blogging engine, I have become significantly more familiar with PHP scripting. Like WordPress, pMachine is written in PHP, so there are significant similarities between the two blogging engines.

Without having to spend too much time reading the documentation, I was able to determine that the comment form was added to my blog items through the following line of code:

<?php weblog_comment_form($id); ?>

This PHP tag retrieves the comment form code and inserts it in the web page as its being built. If the option to require registration first is checked, this tag uses the form for registered users. If the option is not checked, this tag uses the form for unregistered users if the current user is not logged in.

The tag appears in the “comment.php” file that pMachine uses to build blog web pages that include the comment form. I figured that if I could just add some PHP code that tests to see if the user is registered (and logged in) first before actually using the tag to insert the form, I’d be all set.

So I just decided to scan through the existing PHP code in pMachine to see if I could find some code that looked like it was doing just that. I thought about it a bit, and figured that the best place to look would be the place where the code that pMachine uses to build the “MEMBERS” section in the left-hand side menu in this site’s design. After all, this section does exactly what I needed: if the user is not logged in, it displays a “Log-In” link. If the user is already logged in, it displays a “Log-out” link.

I found that the code used to do this is located in the “member.fns.php” — more specifically in the “member_login_navbar()” function. So I just read that function carefully and figured that all I needed to use was the code that loads the global variables for the member and the if clause that tests to see if the reader is actually a member.

And I came up with this to replace the <?php weblog_comment_form($id); ?> line mentioned above:

<?php

global $member_code, $member_name;
if ((isset($member_name)) && (isset($member_code))) 
{  
weblog_comment_form($id);
}
else
{
?>

<div class="comments">

<p><strong>Want to add your own comment?</strong></p>

<p>Due to <a href="https://www.betalogue.com/index.php?p=1362">
comment spam issues</a>, registration is required. 
If you are not registered, click 
<a href=http://www.latext.com/pm/members/register.php">
here</a> to register first.</p>

<p>If you are already registered, click 
<a href="http://www.latext.com/pm/members/login.php">
here</a> to log in.</p>

</div>

<?php
}
?>

I double-checked my syntax, since it’s PHP code inside a web page, i.e. mixed with HTML.

I saved the “comment.php” file and uploaded it to my server. I launched Camino so that I wouldn’t have to log out in Safari to see the effect. I loaded a page with a “COMMENTS” section… and it worked!

I was not logged in in Camino, so I got the “Want to add your own comment?” section. I went back to Safari and loaded the same page. I was logged in in Safari, so I got the comment form directly. Yey! Not bad for a first try…

So there you go. A few lines of PHP code is all that is necessary to change pMachine’s default interface for comment forms to a more user-friendly one. Given that pMachine has been discontinued, there absolutely no point in submitting this to the company as an enhancement request. But if I post it here it might be useful to other pMachine users.


One Response to “pMachine: Customizing the comment form when registration is required”

  1. Yvonne says:

    I just wanted to let you know that pMachine already has a function that checks to see whether a user is logged in or not – it’s called not_logged_in(). :)

Leave a Reply

Comments are closed.