Extended Customization of the WordPress Comment Form

Preparing this blog to become GDPR compliant, I wanted to customize the comment form to only ask for the comment itself, plus having a checkbox that the author agrees with the storage and handling of the data. In the background, the author’s IP address should not be stored any more. And finally, I wanted to change the text on top of the form from “Leave a Reply” to “Leave a Comment“.

This is how the original form looked like.

Original Comment Form

And here is the customized form, as looks like after implementing the changes described by this post.

Customized Comment Form

The initial information on how to prepare this blog for GDPR compliance gave me Sonia Rieder’s german Webtimiser post.

The base information on how to implement these changes I found at wpbeginner’s “How to Style the WordPress Comment Form (Ultimate Guide)“. According to wpbeginner’s recommendation, I did not changed the theme, but implemented my own (and first) WordPress plugin. A guide on how to do this is, of course, is also provided by wpbeginner: “What, Why, and How-To’s of Creating a Site-Specific WordPress Plugin“.

From these posts I learned that WordPress uses filters to customize a few things. Luckily, I was not forced to fiddle around with the theme code itself, risking that the next theme update will override my changes.

But the samples did not showed me what I have to do to change the form’s title and how to remove the obsolete information that the e-mail address will not be published. So I downloaded the WordPress installation from my Website, took my currently preferred PHP editor Visual Studio Code, and searched for the text to be replaced / changed, e.g. “Leave a Reply“. Beside many matches found in the translation files, which I ignored as I do not use translation, I found the relevant matches in <source path>\wp-includes\comment-template.php

Setting Comment Form Defaults

Here I learned that the obsolete info “Your email address will not be published” is added to an array named $default (not visible in the screenshot), having the key comment_notes_before (lines 2413-2420).

On line 2428 I saw that the “Reply” text is added to the same array having the key title_reply_to, and that I also should change the value of key title_reply_to (line 2430).

A little bit below, on line 2451, I saw that there is also a filter call implemented for the $defaults array, which enables me to create a custom filter function to change these values. The name of this filter is comment_form_defaults.

As there are no fields left in the form for name, e-mail address, or Website, there is no need to filter theses values out before saving the data to the database. Only the author’s IP address has to be removed before saving.

Based on this, I implemented the plugin code:

<?php
/*
Plugin Name: Instance Factory custom functions
Plugin URI: https://instance-factory.com/?p=1546
Description: Contains custom functions to be GDPR complient
Author: Instance Factory, a project of the proccelerate GmbH
Author URI: https://proccelerate.de
Version: 1.8
*/

/* Disable direct access to this file */
if ( ! defined( 'ABSPATH' ) ) exit;
/* OK, start editing! Happy publishing */

function ifb_remove_commentsip( $comment_author_ip ) {
    return '';
    }
add_filter( 'pre_comment_user_ip', 'ifb_remove_commentsip' );

function ifb_remove_comment_fields($arg) {
    $arg['author'] = '';
    $arg['url'] = '';
    $arg['email'] = '';
    return $arg;
}
add_filter('comment_form_default_fields', 'ifb_remove_comment_fields');

function ifb_filter_comment_defaults($defaults) {
    $defaults['comment_notes_before'] = '';
    $defaults['title_reply'] = 'Leave a Comment';
    $defaults['title_reply_to'] = 'Leave a Comment to %s';
    return $defaults;
}
add_filter('comment_form_defaults', 'ifb_filter_comment_defaults');

/* That’s all, stop editing! Happy publishing */
?>

Maybe you’ve noticed that none of these changes will bring up the “Agree …” checkbox. For this feature, I use the “WP GDPR Compliance” plugin by Van Ons et al.

Links

Sonia Rieder’s german Webtimiser post about preparing WordPress for GDPR
wpbeginner’s “What, Why, and How-To’s of Creating a Site-Specific WordPress Plugin
wpbeginner’s “How to Style the WordPress Comment Form (Ultimate Guide)
WP GDPR Compliance” plugin by Van Ons et al.