FastComments.com

Add Live Commenting to Craft CMS Sites

Craft CMS does not include a commenting system. FastComments is added to a Craft site by placing the live commenting widget code in the Twig template that renders entries, so each entry gets its own comment thread.

The template code in this guide uses Twig and entry properties available in Craft 4 and Craft 5. Editing templates requires access to the project's templates/ folder.

This guide requires a FastComments account. You can create an account here.

Find the Entry Template Internal Link

Channel and structure sections give their entries URLs through the Entry URI Format setting. When Craft matches a request to an entry, it renders the section's designated Template and provides that template with an entry variable.

  1. In the control panel, go to Settings → Sections.
  2. Click the section whose entries should have comments, for example a blog channel.
  3. Note the path in the section's Template setting. The template file is located in the project's templates/ folder.

Adding the widget to this template limits comments to entries in that section. To add comments to entries in several sections, either repeat the next step in each section's template, or add the widget to a shared layout as described in Adding the Widget to a Shared Layout.

Adding the Widget to a Shared Layout Internal Link

When several sections share a layout template, the widget can be added to the layout once instead of to each entry template. The layout is also rendered for templates that do not belong to an entry, so wrap the code in a check for the entry variable:

Craft CMS Layout Snippet
Copy Copy
1
2{% if entry is defined %}
3 <div id="fastcomments-widget"></div>
4 <script src="https://cdn.fastcomments.com/js/embed-v2.min.js"></script>
5 <script>
6 window.FastCommentsUI(document.getElementById('fastcomments-widget'), {
7 tenantId: 'demo',
8 urlId: '{{ entry.id }}',
9 url: '{{ entry.url|e('js') }}',
10 pageTitle: '{{ entry.title|e('js') }}'
11 });
12 </script>
13{% endif %}
14

Craft provides the entry variable when it renders a request for an entry's URL. This includes entries in single sections, such as a homepage built as a single, so those pages also show the widget. To limit comments to specific sections, add the widget to those sections' templates as described in Add the Widget to the Entry Template instead.

A template that assigns its own variable named entry with {% set %} also passes this check.

Use either this method or the per-section method, not both, so the widget is not output twice on the same page.

Customization Internal Link

The widget's styling and behavior can be changed to match your site, either in the FastComments widget customization page or by adding options to the configuration object in the snippet.

See the Customization Documentation for the available options.

To show signed-in Craft users as the comment authors, use FastComments Secure SSO. The SSO verification hash is an HMAC-SHA256 hash computed with your FastComments API secret, so it must be generated in server-side PHP code and passed to the template. Do not compute it in browser JavaScript or expose the API secret in a template's output.