How To Add Comment Box In WordPress Theme With The Clean Code?

how to add comment box in wordpress theme

One of the hardest parts of a WordPress Theme is the comments box. Do you want to know how to add comment box in WordPress Theme?

You all know that developing a WordPress Theme isn’t a piece of cake. It requires expertise and devotion to keep going with the codes.

You can get the WordPress comment form with one line of the code. But to make it function properly, you have to write many lines of the codes.

Whether you create a brand new theme or you create a child theme. You have to add an author box, search box, comment box etc.

Your Question “how to add comment box in WordPress Theme has an answer”.

In this tutorial, I will provide the clean code with the possible security. Adding a comment box in WordPress would take you to the completion of your WordPress Theme.

I have experienced that it would take quite a few time to understand the coding of this comment box. If you want to use the default WordPress comment box to your website then it’s the best way.

There are many other comment systems like Disqus and Facebook.

What Should You Do To Add Comment Box In WordPress?

If you are aware of WordPress Theme file structure then you would also know about a file named “comments.php“. In each WordPress Theme, this file is present which controls the functioning and the structure of the comment box.

If you are developing a new theme or adding a comment box in an old theme, you have to create a new file “comments.php” in the theme folder.

You can edit the theme from the theme editor or using the cPanel.

Add the code in the file.

Let me provide you the code.

<?php
if ( post_password_required() ) {
return;
}
?>
<div id=”comments” class=”comments-area”>
<?php
if (have_comments() ) :
//We have comments
?>
<h2 class=”comment-title”>
<?php
printf(
esc_html( _nx( ‘One Comment’, ‘%1$s comments’, get_comments_number(), ‘comments title’, ‘blogginglovetheme’) ),
number_format_i18n( get_comments_number() )
);
?>
</h2>
<ol class=”comment-list”>
<?php
$args =array ( ‘walker’ => null, ‘max_depth’ => ”, ‘style’ => ‘ol’, ‘callback’ => null, ‘end-callback’ => null, ‘type’ => ‘all’, ‘reply_text’ => ‘Reply’, ‘page’ => ”, ‘per_page’ => ”, ‘avatar_size’ => ’48’, ‘short_ping’ => false, ‘echo’ => true );
wp_list_comments($args);
?>
</ol>
<?php
if( !comments_open() && get_comments_number() ):
?>
<p class=”no-comments”><?php esc_html_e( ‘Comments are closed.’, ‘blogginglovetheme’); ?></p>
<?php
endif;
?>
<?php
endif;
?>
<?php comment_form(); ?>
</div>

This code will generate a comment form and all its functionalities. Every website has its different comment box. Depending on your choice, you can design the comment form using CSS.

Before saving, it is recommended to arrange the code properly. Use the “TAB” coding to maintain spacing and the size of the file. You just have to arrange the code properly.

It’s important to add the clean code so that anyone can understand it.

There will be a heading showing the number of comments. The comment author and the possible details of the author with the date.

If you want to show the date according to you then you can change it using different PHP date rules.

When you start developing a WordPress Theme, you install WordPress on Xampp server and feel happy to build each segment of the theme. But when the time comes to add comment box in WordPress, it would be really hard to understand its concept.

The question “how to add comment box in WordPress Theme” would make you sick. And the most disappointing part is that you don’t find any relevant code to solve the problem.

But here I am with you all, just copy the code shown above and paste it in the comments.php file of the theme.

NOTE:- This comment box code won’t show the comment box. You have to call the template wherever you want to show it.

If you want to show the comment box under the single post or the pages, you have to call this comments’ template.

Let me provide you the code.

<?php comments_template(); ?>

Add this code in the single.php if you want to add comment box in WordPress single posts. If you want to show the comment box on the pages then you can add this code in page.php file.

It’s your choice whether you want to show the comment’s box on the pages or not. You may have noticed that most of the websites don’t have the comments on their “About Us” page.

Are You Still Searching To Know How To Add Comment Box In WordPress Theme?

Web Development is an amazing field and you would be glad to know the code to add comment box in WordPress theme. As of my experience, adding a comment box in WordPress Theme is one of the complicated parts.

There are many conditions to build. Like if you have comments then what to provide, if you don’t have the comments then what.

The code shown in this tutorial can be used in any WordPress Theme. There are many inbuilt IDs and classes of the HTML elements to target for the design of the comment box. You can provide a beautiful design to your comment box.

by Ravi Chahar

A WordPress Professional and the LinkedIn Influencer. A coder by passion and a blogger by choice. WordPress theme development is his forte. He is your WordPress guy who will teach you how to solve WordPress errors, WordPress security issues, design issues and what not.

Get Free Updates Into Your Inbox

Learn Everything Just Like I Did

SUBSCRIBE

9 comments

  1. Hi Ravi,

    Informative indeed ?

    I know it all consists of codes, but it would certainly be worth it, especially if you are looking for a clean code kind of comment box, as other commenting systems do have their drawbacks. Need to take a note of it and so I would bookmark it to use it if required in the near future.

    Thanks for sharing it with us. Have a nice week ahead ?

    1. Hey Harleena,

      In the WordPress comment box, most of the WordPress functions are used with the proper loops. But in the other commenting system, it’s hard say anything. No one knows whether they use the scripts or just the simple format.

      So it’s important to have a well-coded comment box.
      Thanks for stopping by.
      Enjoy the day.
      ~Ravi

    1. Hey Ryan,

      Adding a comment box is one of the hardest parts of a theme development process. The code mentioned above consists of many loops and conditions.

      I am sure, there are people around us who need to have such codes for their web development.

      Glad to see you again.
      Have a great week.
      ~Ravi

  2. Yowza, Ravi! That’s a lot of coding going on. I so admire your tutorials. They are easy to understand and very informative.

    Hope you are having a great week! Thanks for sharing this with us!

    B

    1. Hey Bren,

      WordPress coding is a kind of really easy to understand. If you try to learn about it then it would be really interesting. We all know that comment box is important so that our readers can share their opinions.

      Thanks for taking the time to comment.
      Enjoy the day.
      ~Ravi

  3. Hi Ravi,

    Wonderful post. I was struggling with this issue and started using disqus system.

    But now I can give a shot ?

    1. Hey Vikas,

      If you want to use the Disqus or the Facebook comment system then you don’t need to worry about the codes. Just use the plugins and a nice comment box would be there.
      Thanks for stopping by.
      ~Ravi

    2. Hello Vikas,
      Sometimes in comment we can find very nice additional content for our post. As i know Disqus or Facebook are problem for Google Robot because theyr content is loaded by AJAX.

      Btw, thank You Ravi for this tutorial!

Leave a Reply

Your email address will not be published. Required fields are marked *