Comments fields (FieldtypeComments)

Making use of the Comments fieldtype

1. Make sure that Comments fieldtype (FieldtypeComments) is installed from the Admin > Modules menu.

2. Now go to Admin > Setup > Fields and create a new field. Select "Comments" as the fieldtype and give it whatever name you wish (I usually call it just "comments", and code snippets here assume it's called that).

3. On the next screen, you'll have several options to configure the comments field. Make sure that you enter a Notification email. This is the email address that gets notified when new comments are submitted. Currently it is the only way you will know when new comments are posted (short of checking the admin page yourself), so using this is important.

4. Now edit the template where you want your comments to appear. Enter this:

<?php echo $page->comments->render();

5. Likewise, in your template, enter this where you want the comments entry form to appear:

<?php echo $page->comments->renderForm();

6. Your comments are now ready to use. Note that any templates that use comments for posting should not use Template caching unless you configure the comments to post to another template/page. You'll see an option for this in the renderForm() options shown further down on this post.

Adding the Akismet Spam Filter

If you'd like to reduce spam submissions, we recommend that you use the Akismet spam filter with your comments, when possible. To do so, you'll need an Akismet API key.

Once you have it, click to Admin > Modules > Akismet (CommentFilterAkismet). Enter your API key in the space provided and Save. Then go to Setup > Fields > and edit the comments field you added. Check the box for "Use Akismet". You'll also see a field that says how many days it will keep spam before deleting it. I usually set this at 1 or 2 days, just in case Akismet identifies something as spam that isn't (though it's yet to happen!). Hit Save, and your comments are now hooked into Akismet.

OPTIONAL: Customizing the Comments Output

You may want to change some of the basics of the comments output. You can do this by passing arguments to the functions you called above to render the comments list and comments form. Included below are examples with all options. You may specify any one or more of the options -- only specify the ones you want to change:

<?php
// comments list with all options specified (these are the defaults)
echo $page->comments->render(array(
    'headline' => '<h3>Comments</h3>',
    'commentHeader' => 'Posted by {cite} on {created}',
    'dateFormat' => 'm/d/y g:ia',
    'encoding' => 'UTF-8',
    'admin' => false, // shows unapproved comments if true
));

// comments form with all options specified (these are the defaults)
echo $page->comments->renderForm(array(
    'headline' => "<h3>Post Comment</h3>",
    'successMessage' => "<p class='success'>Thank you, your submission has been saved.</p>",
    'errorMessage' => "<p class='error'>Your submission was not saved due to one or more errors. Please check that you have completed all fields before submitting again.</p>",
    'processInput' => true,
    'encoding' => 'UTF-8',
    'attrs' => array(
        'id' => 'CommentForm',
        'action' => './',
        'method' => 'post',
        'class' => '',
        'rows' => 5,
        'cols' => 50,
        ),
    'labels' => array(
        'cite' => 'Your Name',
        'email' => 'Your E-Mail',
        'text' => 'Comments',
        'submit' => 'Submit',
    ),
    // the name of a field that must be set (and have any non-blank value), typically set in Javascript to keep out spammers
    // to use it, YOU must set this with a <input hidden> field from your own javascript, somewhere in the form
    'requireSecurityField' => '', // not used by default
    ));

Lets say that you just wanted to change the headlines for the comments list and comments form. You'd do this:

<?php
echo $page->comments->render(array(
    'headline' => '<h2>Read Comments</h2>',
    ));
echo $page->comments->renderForm(array(
    'headline' => '<h2>Join The Discussion</h2>',
    ));

OPTIONAL: Generating your own output

If you wanted to generate your own output (rather than use the built in render() methods), you can iterate the $page->comments field:

<?php
foreach($page->comments as $comment) {
    if($comment->status < 1) continue; // skip unapproved or spam comments
    $cite = htmlentities($comment->cite); // make sure output is entity encoded
    $text = htmlentities($comment->text);
    $date = date('m/d/y g:ia', $comment->created); // format the date
    echo "<p><strong>Posted by $cite on $date</strong><br />$text</p>";
}

You can likewise do the same for the comment form. But I don't want to get too far into this level yet since this is supposed to be a quick guide.

Styling the Comments

Below is a starting point for styling the comments. It's just pulled from the comments styling at processwire.com, so it should be seen as an optional starting point rather than as a ready-to-go stylesheet. Or, you may prefer to start from scratch on styling the comments, depending on your need.

.CommentList {
        margin: 1em 0 0 0;
}
    .CommentListItem {
            list-style: none;
            margin: 0;
            border-left: 5px solid #ddd;
            border-top: 1px dotted #ccc;
            padding: 1em 0 1px 1em;
            background: #fff;

    }
        .CommentListItem p {
                margin-top: 0;
        }
        .CommentHeader,
        #CommentForm label {
                margin: 0;
                font-weight: bold;
                font-size: 11px;
                text-transform: uppercase;
                color: #3786bc;
        }
        .CommentText {
                color: #666;
        }
#CommentForm {
}
    #CommentForm h2 {
            margin-bottom: 0;
            border: none;
    }
    #CommentForm label {
            display: block;
            color: #999;
    }
    #CommentForm p {
            margin: 0.5em 0;
    }
    #CommentForm .error {
            background: #a30000;
            color: #fff;
            padding: 0.25em 0.5em;
    }
    #CommentForm .success {
            font-weight: bold;
    }
    .CommentForm_cite,
    .CommentForm_email {
            float: left;
    }
        .CommentForm_cite input,
        .CommentForm_email input {
                width: 200px;
                margin-right: 1em;
        }
    .CommentForm_text {
            clear: both;
    }
        .CommentForm_text textarea {
                padding: 2px;
                width: 75%;
                height: 100px;
        }

Twitter updates

  • This post covers a few of the bigger updates in ProcessWire 3.0.154+3.0.155. This includes new live replacement of text in core and modules, a new method for creating canonical URLs, and some major upgrades to our input->urlSegment() method! More
    24 April 2020
  • A brief look at what's new in ProcessWire 3.0.154 in this forum post: More
    17 April 2020
  • This week we’ve got a few new and interesting core updates in progress, though none quite ready to release just yet. So rather than releasing version 3.0.154 today, I thought we'd instead take a brief look at what’s coming over the next few weeks… More
    3 April 2020

Latest news

  • ProcessWire Weekly #311
    In the 311th issue of ProcessWire Weekly we're going to talk a bit about the latest dev versions of ProcessWire, introduce a couple of new third party modules, and more. Read on!
    Weekly.pw / 25 April 2020
  • ProcessWire 3.0.154 and 3.0.155 core updates
    This post covers a few of the bigger updates in ProcessWire 3.0.154 and 3.0.155 on the dev branch. This includes a new function for live replacement of text in core and modules, a new method for creating canonical URLs, and some major upgrades to our $input->urlSegment() method that I think you’ll like!
    Blog / 24 April 2020
  • Subscribe to weekly ProcessWire news

“Yesterday I sent the client a short documentation for their ProcessWire-powered website. Today all features already used with no questions. #cmsdoneright—Marc Hinse, Web designer/developer