How To Add Comment Function Without Plugins

Comment Box

Whenever you share a post on your WordPress website, then someone can comment if you want to comment on that post. Then it is necessary to have a comment box to comment.

If you want to put a comment box or comment function in your WordPress theme, then follow the steps given below.

1 – function.php
First, you paste the code below in function.php.

/*
* Change the comment reply link to use 'Reply to <Author First Name>'
*/
function add_comment_author_to_reply_link($link, $args, $comment){

$comment = get_comment( $comment );

// If no comment author is blank, use 'Anonymous'
if ( empty($comment->comment_author) ) {
if (!empty($comment->user_id)){
$user=get_userdata($comment->user_id);
$author=$user->user_login;
} else {
$author = __('Anonymous');
}
} else {
$author = $comment->comment_author;
}

// If the user provided more than a first name, use only first name
if(strpos($author, ' ')){
$author = substr($author, 0, strpos($author, ' '));
}

// Replace Reply Link with "Reply to <Author First Name>"
$reply_link_text = $args['reply_text'];
$link = str_replace($reply_link_text, 'Reply to ' . $author, $link);

return $link;
}
add_filter('comment_reply_link', 'add_comment_author_to_reply_link', 10, 3);

2 – comments. php
Now you Create comment.php file and paste given code

<?php
/**
* The template for displaying comments
*
* This is the template that displays the area of the page that contains both the current comments
* and the comment form.
*
* @link https://codex.wordpress.org/Template_Hierarchy
*
* @package WordPress
* @subpackage Twenty_Seventeen
* @since 1.0
* @version 1.0
*/
/*
* If the current post is protected by a password and
* the visitor has not yet entered the password we will
* return early without loading the comments.
*/
if ( post_password_required() ) {
return;
}
?>
<div id="comments" class="comments-area">
<?php
// You can start editing here -- including this comment!
if ( have_comments() ) : ?>
<h2 class="comments-title">
<?php
$comments_number = get_comments_number();
if ( '1' === $comments_number ) {
/* translators: %s: post title */
printf( _x( 'One Reply to &ldquo;%s&rdquo;', 'comments title', 'nd_dosth' ), get_the_title() );
} else {
printf(
/* translators: 1: number of comments, 2: post title */
_nx(
'%1$s Comment ',
'%1$s Comments ',
$comments_number,
'comments title',
'nd_dosth'
),
number_format_i18n( $comments_number ),
get_the_title()
);
}
?>
</h2>
<ol class="comment-list">
<?php
wp_list_comments( array(
'avatar_size' => 100,
'style' => 'ol',
'short_ping' => true,
'reply_text' => __( 'Reply', 'nd_dosth' ),
) );
?>
</ol>
<?php the_comments_pagination( array(
'prev_text' => '<span class="screen-reader-text">' . __( 'Previous', 'nd_dosth' ) . '</span>',
'next_text' => '<span class="screen-reader-text">' . __( 'Next', 'nd_dosth' ) . '</span>',
) );
endif; // Check for have_comments().
// If comments are closed and there are comments, let's leave a little note, shall we?
if ( ! comments_open() && get_comments_number() && post_type_supports( get_post_type(), 'comments' ) ) : ?>
<p class="no-comments"><?php _e( 'Comments are closed.', 'nd_dosth' ); ?></p>
<?php
endif;
comment_form();
?>
</div><!-- #comments -->

3 – style.css
Now you paste the code below in style.css

#respond h3, .comments-title{
color:black;
padding:20px;
font-size:20px;
border-top:1px solid #000;
border-bottom:1px solid #000;
margin:30px 0;
}
#commentform .comment-notes{
margin-bottom:20px;
}
#commentform label{
display:block;
}
#commentform input[type="text"],
#commentform input[type="url"],
#commentform input[type="email"],
#commentform textarea{
border: 1px solid #ccc;
border-radius: 4px;
margin-bottom: 20px;
width: 100%;
background-color: #f8f8f8;
padding: 7px 10px;
font-size: 18px;
}
#commentform input[type="submit"]{
line-height: 38px;
border-radius: 4px;
padding: 0 20px;
background-color: #fdb813;
color: black;
font-weight: bold;
font-size: 16px;
border:0;
position: relative;
transition:all 0.4s;
cursor:pointer;
}
#commentform input[type="submit"]:hover{
background-color: #6e551a;
color: white;
}
.comment-body .comment-author{
float:left;
width:30%;
}
.comment-body .comment-author .fn{
display:block;
margin-top:10px;
}
.comment-body{
overflow:hidden;
margin-bottom:30px;
}
.comment{
margin-bottom:30px;
}
.comment-list, .children{
margin:0;
padding:0;
list-style-type: none;
}
.comment-list > li:first-child{
border-top:0;
padding-top:0;
}
.comment{
border-top:1px solid #ccc;
padding-top:30px;
position:relative;
}
.children .comment{
border-top:0;
}
.comment .children{
border-top:1px dotted #5e666b;
border-left:1px dotted #5e666b;
padding-top:10px;
padding-left:10px;
}
.children{
margin-left:100px;
}
.children img{
max-width:75px;
height:auto;
}
.children .fn{
font-size:12px;
}
.says{
display:none;
}
.comment-metadata{
margin-left:30%;
}
.comment-metadata a{
color:#5e666b;
font-size:14px;
margin-bottom:10px;
display:block;
}
.comment-reply-link{
position:absolute;
top:15px;
right:0;
font-size:12px;
color:#1a3794;
}

4 – single.php
Now paste the below code into single.php

<?php
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
?>

Add Human validation function using php code  – Click Here

Watch Video

In this way, you can put a comment box without any plugins.