How to Add Custom Text Around WordPress Comments

After the 30 days decision, the first change I wanted to make immediately to the site was letting readers know that comments will be closed after certain period. I didn’t get into details before, WordPress simply doesn’t load comments section if the post in question doesn’t have any comments and the submission is closed. In other words, there is “around the comments” control for the site admin or the site builder. Any actions would need to be hooked to the body of the contents or beginning of the footer.

Fortunately, WordPress does have several hooks even within the comment form itself. I recommend using two notice boxes to communicate why the submission field is closed, and if it still open when it will be closed. This one is about when it will be closed on the field.

  1. Identify where you would want the message to be displayed. WordPress has several hooks in and around the comment submission section. (e.g. comment_form_before for above the box, comment_form_top for below the comment box title, comment_form_after for after the box)
  2. Replace the SOME_HTML_TEXT to your liking in HTML, and WHERE_IN_COMMENT_FORM to the hook from step 1 in the following code.
function comment_form_custom_message(){
	?>
	SOME_HTML_TEXT
	<?php
}
add_filter('WHERE_IN_COMMENT_FORM', 'comment_form_custom_message');

Here is an example of what I have done with mine.

function comment_form_custom_message(){
	?>
	<p class="comments-form-notice">Comments will be automatically closed after 30 days.</p>
	<?php
}
add_filter('WHERE_IN_COMMENT_FORM', 'comment_form_custom_message');
  1. Insert the edited code to the end of the functions.php in the child theme.
  2. (opt) Using the stylesheet, you can change the fonts, padding, and etc. for the message box.

To clarify what works inside the function, any HTML codes will do. I’ve used <p> tag only to help with the stylesheet. It could be in different class name, or it could be some other HTML element.

Leave a comment

Comments will be automatically closed after 30 days.