WordPress comment form modification
This site uses WordPress system to manage contents.
On article pages of this system, we can write comment for the article at a form that is displayed like the right (or following, if you uses smart phone) snapshot. Somehow, this form contains 'Website' field by default. But isn't this really necessary?
On the other hand, the beginning message that says "your email address is…" might be requested to add a reference link to the privacy policy page if it is prepared.
These are why I have started modification of the comment form.
Standard comment form
Only hiding the 'Website' input item, we can do it easily by adding the following setting into the additional CSS setting in the WordPress customization.
.comment-form-url{ display:none; }
However, even with this, the "Save my name and ..." message still contains 'website' word.
Additionally, it seems impossible to tweak the message before or after the "Your email address will not be…" only by adding some CSS settings.
So, I decided to take the plunge and fix the problem by modifying the code as I will explain later, which allowed me to create the form shown on the right (or below if you are viewing this on a smartphone).
Customized comment form
On modification, we should not to modify original code of the WordPress in order to prepare for future updates of the WordPress system. So, the modifications are done on the child theme code which prepared separately. I had used 'add_filter()' function to add filter hook which applies additional feature to the original WordPress function.
This time, I have added two filter hooks for the following WordPress functions:
(1) comment_form_default_fields()
Here, the 'Website' column is hidden regardless of the CSS settings, and the message of the "Save my name..." is modified according to the displayed language that can be identified by the get_locale() function.
(2) comment_form_defaults()
Here, the opening display message is modified according to the displayed language.
Although I had tried to combine processes (1) and (2) into one of the functions, but that didn't seem possible, so I chose to do them separately.
add_filter('comment_form_default_fields', function($fields) {
// Hide 'website' input field.
unset($fields['url']);
// Remove web site from cookie message
$cookies = $fields['cookies'];
switch(get_locale()) {
case 'ja': // Japanesen
$cookies = str_replace('、サイト','',$cookies);
break;
case 'en_GB': // English
$cookies = str_replace(', email, and website',' and email',$cookies);
break;
default:
break;
}
$fields['cookies']=$cookies;
return $fields;
});
add_filter('comment_form_defaults', function($fields) {
switch(get_locale()) {
case 'ja': // Japanese
$add_note = 'ご記入いただく個人情報の取り扱いについては<a href="' . home_url('/privacy-policy') . '">プライバシーポリシー</a>をご参照ください。';
break;
case 'en_GB': // English
$add_note = 'Regarding to handling of enterd personal information, refer <a href="' . home_url('/privacy-policy') . '">Privacy Policy</a> page.';
break;
default:
break;
}
$dst_tag = '<span id="email-notes">';
$fields['comment_notes_before'] = str_replace($dst_tag, $dst_tag . $add_note . '</br>', $fields['comment_notes_before']) ;
return $fields;
});
In the actual comment form of this site which will be displayed later, a character authentication function has been added to avoid spam posts.