The ability to add a comment reply. WordPress Comment Template. Discussions are closed for this page

This name does not interfere with many, but sometimes the question arises how to change add a comment to leave a review or the like. In this case, this article will help you. As I wrote above, I will show a couple of ways.

Method number 1

For the first method, you must find the file in the folder with the theme that is installed on your site. comments.php. If there is one, open it and look in it for an array responsible for setting up and displaying the content of the comment form. It looks something like this:

$args = array("comment_notes_before" => "

", "comment_field" => "

", "id_submit" => "comm_subm", "label_submit" =>

So, to this array, you need to add a new parameter with the desired value and that's it. In our case, this is:

"title_reply" => "Leave your own review",

How do you understand - Leave your review, this is the new name instead of - Add a comment. You can add such a parameter to the end or to the beginning of the array. It will turn out something like this:

$args = array("title_reply" => "Leave your comment", "comment_notes_before" => "

Fill in the fields below. Your e-mail will not be published. Required fields are marked *

", "comment_field" => "

", "id_submit" => "comm_subm", "label_submit" => "Submit",); comment_form($args);

Added to the beginning and now, on the pages of entries, a line will be displayed - Leave your review. This method is not complicated and requires minimal effort, the main thing is to correctly define the array. But sometimes it happens that there is no comments.php file inside the theme or there is no array in it and you don’t know how to add it there correctly, then the second method will help you.

Method number 2

The essence of the second method is to add a new function, which will make the replacement of the name. To do this, in the folder with the active theme, you need to find the file functions.php with custom functions and in a convenient place, if you don't know this, then at the very end before the PHP closing tag - ?> , if there is none, then just at the very end, add the following code:

Function wph_change_submit_label($defaults) ( $defaults["title_reply"] = "Submit Your Feedback"; return $defaults; ) add_filter("comment_form_defaults", "wph_change_submit_label");

Bonus

This is the same, in fact, what was done in the first method, simply, there we embedded directly into the array, and here through the filter. After these steps, you will see a change in the title. As in the first method, you can change the text - Leave your review to the one you like. This text is provided as an example.

I also want to suggest and pay attention to the declension of the word Comments. If you have a line in the form - 1 comment or Comments: 10 posted. You can use the Function from the article. If you are already using it, then I recommend in line with an array:

Array("comment","comments","comments")

Change the titles to review,review,reviews if you used that word in the methods above. thus, everything will be the same for you, and not in a scatter, there are reviews, and there are comments.

This is the information I would like to share with you today. Perhaps it will be useful to someone.

That's all, thanks for your attention. 🙂

Hello, friends! This article will show you how to see comment replies via mail to your commenters.

Replies to comments via the Comment Reply Notification plugin

Despite the fact that today is December 31 and it's time to celebrate the New Year 2016, there is a need to write an article on the blog, because no one has canceled the publication schedule. The published article is not New Year's at all, the congratulatory article was published a little earlier. Taking this opportunity, I congratulate you, dear readers of the blog, on the upcoming New Year, I wish you good health, happiness, good luck and prosperity. You can read a congratulatory article, see photos and videos on the blog.

During the training in the Expert Group (EG), many students asked questions about installing and configuring the Comment Reply Notification plugin, the blog readers had the same question. More precisely, there was a question about how to make your blog commentator see your replies to comments. There is no article on this topic on the blog, but I think today's article will correct this situation.

So, a reader made a comment on your blog. Naturally, you or another reader responds to it, but the commentator may not find out about this and will not write his new answer. To fix this situation, there is a Comment Reply Notification plugin that sends responses to comments by mail. That is, the reader wrote a comment on your blog, you answered it. Now the Comment Reply Notification plugin immediately notifies the commenter by mail that his comment has been answered.

As a rule, the commenter returns to the blog and writes new replies to the comments. For your blog, this is very good:

  • firstly, new comments appear, search engines believe that the article is interesting and raise its significance;
  • secondly, new comments start a discussion and new commentators are involved in the commenting process;
  • thirdly, writing comments (responding to comments) takes time, which means people will stay on the blog more, improving behavioral factors;
  • fourthly, constant commentators will increase the core of your blog's audience.

As you can see, it is important to communicate responses to comments to people via email. Therefore, you need to install the Comment Reply Notification plugin on your blog.

Installing the Comment Reply Notification plugin is quite simple and is shown in the video below. Next, the Comment Reply Notification plugin is activated and its settings are made. Click in the control panel "Settings" and "Comment Reply Notification", go to the plugin settings page. We connect the function "Always inform", in the field "Settings for the subject of e-mail notification letter", we prescribe the name of our blog (site).

Now you need to customize the text of the letter itself. It is presented as a code in English. For the correct display of the letter in Russian, which will be sent to the commentator, it is necessary to replace a number of English phrases with Russian ones. Below are the phrases that you can insert into the letter template, if you wish, you can change them:

  1. Your comment on the post.
  2. Got a new answer.
  3. Here is your comment.
  4. Here's a new answer.
  5. You can see more comment information for this article here.
  6. Thanks for the comments.
  7. Comment Reply Notification.
  8. This email was sent automatically, please do not reply to it.

The replacement itself is shown in the attached screenshots:

Phrases that are underlined in red should be replaced with Russian ones (shown above).

It should turn out like this.

After changing the text of the letter, click the "Save settings" button. This completes the work on setting up the Comment Reply Notification plugin. Watch the video showing the process of installing and configuring the Comment Reply Notification plugin:

Now commentators will automatically receive emails, and they will see the replies to comments immediately. Use this amazing plugin to promote your blog. Good luck to you!

Get new blog articles straight to your inbox. Fill out the form, click the "Subscribe" button

I recently dug into the files of my WordPress theme, namely the rules for the comment output template, along the way understanding its structure and various functions responsible for displaying comments on blog posts. As a result, I changed the standard output, created and included my own comments.php file. I decided to issue the result in the form of an article, as I understood this topic well, and there was quite a lot of material.

I hope this article will be useful for WordPress blog owners familiar with HTML, CSS and PHP.

***

In WordPress, to connect a comment template to a post or page, the comments_template() function is used, which takes two parameters:

  • the first is the path to the template file, by default it is comments.php in the folder with the current theme
  • the second one is used to separate comments by type (regular, trackbacks and pingbacks), false by default

Let's insert comments_template() after post output in single.php post template or page.php page template.

See the WordPress Codex for a description and accepted arguments for the comments_template() function and others mentioned in the article.

Template preparation

Let's try to understand the WP comment templates and create a file for displaying comments on blog posts and pages with our own hands. As examples for reference, you can take templates from standard WordPress themes. Let's create a new document in any text editor, name it comments.php and start editing.

  • In principle, you can name the file whatever you like, and then write the path to this file in comments_template(), but it’s better to stick to the standard name
  • You can edit the file in the WP admin panel, by the way
  • It is best of course to write code and immediately check its action on your blog or on a local server.

In WordPress it is possible to disable comments for individual posts, so before displaying them, you need to check for “openness”:

This is the wrapper code for our next steps. Now let's prepare the container for the comment block

with a semantically correct class or identifier (a class is of course preferable):

Inside

write the title so that your readers understand that there are comments and nothing else, tag

will be just right for this:

"

Here we have specified one of the WordPress functions - the_title() , the result of executing this function will be the title of the current post or page. If you do not want to display the title, then you can simply write "Reader Comments".

Further, before displaying comments, you need to make sure that they are available, i.e. check, if there is - display the full list, if not - then you can show the user something like "". So it will be clear to the visitor of your post / page that no one has written anything yet, and the motivating phrase “You can be the first” will increase the likelihood that they will write something to you faster.

So, after such a statement of the problem, it becomes clear that for implementation we will need if/else constructs and a function to display the number of comments get_comments_number() . If the function returns 0 (zero), then we display "No comments yet ...", otherwise "Readers' comments ...":

There are no comments yet, but you can be the first

Readers' comments on the article ""

Discussions are closed for this page

Output of comments

Great, we have displayed the titles depending on the presence or absence of comments, now it is logical to display the comments themselves - the wp_list_comments() function is responsible for this. The default function wraps all comments in tags

  • , so you should add a wrapper
      with class assignment.commentlist:

      wp_list_comments() accepts an array of arguments with which you can flexibly customize the output of comments. For example, you can change the size of the avatar, the text of the response to a comment, and other settings by passing the keyword and value:

      $args = array("avatar_size" => 64, // avatar size is 64*64px, default is 32 comment type)

      The callback parameter deserves special consideration, which takes the value of the name of the custom comment output function. With its help, you can flexibly customize the appearance of each comment. This is how the standard output function from the comment-template.php file looks like:

    1. id="li-comment-">
      "); ?> %s says:"), get_comment_author_link()) ?>
      comment_approved == "0") : ?>
      $depth, "max_depth" => $args["max_depth"]))) ?>

      The easiest way is to take this function and edit it for yourself, and then call it as a custom one by writing it in the comments.php or functions.php file.

      After listing comments, you can change their appearance through CSS styles. Some wp_list_comments() options are duplicated in the WP admin, Options → Discussion tab, in particular the presence of tree comments, sorting by date, etc.

      Comment submission form

      To add a comment form, use the comment_form() function. Let's add it below the list of comments:

      There are no comments yet, but you can be the first

      Readers' comments on the article ""

      1. 64, "reply_text" => "Reply", "callback" => "my_comments"); wp_list_comments($args); ?>

      Discussions are closed for this page

      With this call, comment_form() will load the default code from the WordPress comment-template.php file. The function takes two parameters:

      Comment_form($args, $post_id);

      • $args - array of form output settings
      • $post_id - id of the post to which the function will be applied, by default the current post

      Let's for example do validation on HTML5 form fields, add text hints. Let's create an array $args to enter the desired settings:

      $args = array(); comment_form($args);

      In the array, you need to write the settings keys:

      $args = array("fields" => apply_filters("comment_form_default_fields", $fields));

      Now we need to fill in the $fields array variable, which includes the form fields. The easiest way is to take the default WordPress code from comment-template.php and modify it a bit:

      "

      " . ($req ? " *" : "") . "

      ", "email" => " ", "url" => "

      " . "

      "); $args = array("fields" => apply_filters("comment_form_default_fields", $fields)); comment_form($args); ?>

      Here, the values ​​of the author , email , and url parameters are the html code of the Name, Mail, and Website fields, respectively. These values ​​need to be edited.

      For the fields, we need to add the following attributes:

      • required - makes the fields mandatory, add it for the "Name" and "Site" fields
      • placeholder - adds a text hint to the field
      • pattern="(3,)" for the "Name" field - specify the name in the letters of the Latin or Russian alphabet and a length of at least 3 characters
      • type="email" for the "Mail" field - this will add HTML5 email validation
      • autocomplete - enables autocomplete for fields
      • type="url" for the "Site" field

      Keep in mind that the new HTML5 attributes won't work in older browsers. Those browsers that do not understand the new field types will simply display them as text, i.e. .

      In addition, for my blog, I swapped tags in some places, added classes for styling, as a result I got the following $fields array code:

      "

      ", "email" => " ", "url" => "

      "); ?>

      We have changed the data entry fields. Now let's edit the comment form itself

      " ?>

      This is the standard WordPress code, I just modified it a little - I added a text hint and added an additional class for styling.

      Here's what I ended up with with CSS styling:

      WordPress Comment Form Using HTML5 Attributes

      Outcome

      Finally, I'll throw off my resulting comments.php code:

      article readers ""

      • Be the first to comment - the author tried
      1. id="li-comment-">
        "); ?> %s writes:"), get_comment_author_link()) ?>
        comment_approved == "0") : ?>
        $depth, "max_depth" => $args["max_depth"]))) ?>
        "Reply", "callback" => "verstaka_comment"); wp_list_comments($args); ?>
      "

      ", "email" => " ", "url" => "

      "); $args = array("comment_notes_after" => "", "comment_field" => "

      ", "label_submit" => "Submit", "fields" => apply_filters("comment_form_default_fields", $fields)); comment_form($args); ?>

      Discussions are closed for this page

      FAQ on comments

      How to highlight author and user comments?

      Sometimes it is very convenient to set a separate appearance for author comments, there are even special plugins for this. However, you can do without any plugins - just by writing styles for the .bypostauthor class in the css file. Similarly, you can set styles for user comments - .bypostuser:

      How to style tree comments?

      To enable tree comments, you need to go to the WP admin, Settings → Discussion → Allow tree comments. Now the child comments will have a tree structure, they can be given separate styles, for example, indented. All you need to do is set the css rules for the list with the .children class:

      Commentlist .children ( padding: 0 0 0 40px; /* left padding for child comments */ )

      Styles for even and odd comments

      WordPress by default gives odd comments the class.even , even comments.odd . Through these classes it is easy to set your own styles:

      Commentlist .even ( /* styles for odd comments */ ) .commentlist .odd ( /* styles for even comments */ )

      How to close comments on a single post?

      Very easy - go to the page for writing a post, Screen settings → Discussions, a Discussions block appears under the post field, deselect the Allow comments item.

      • When compiling your own comment template, you can use the comments.php files from standard and other paid and free WordPress themes
      • An alternative to standard comments is third-party comment form plugins, such as the popular DISQUS
      • It is quite possible to edit the code directly in the comment-template.php file itself, however, in the event of a WordPress update, all code will be overwritten - you will have to edit it again
      • Remember - there is no perfect comment template

      Help the project

      66 votes, average: 4,47 out of 5)

      It's time to get serious about styling WordPress comments. In almost all themes, they are configured by system files, which in turn limits the editing of individual functions. I think many people have come across this when they needed to make changes to the comments, but could not find exactly where it is located. Therefore, it will be better to transfer all the functionality to the current theme, which will give us complete freedom of control.

      In this article, I've put together some cool features that will help improve comments. Still, they allow you to conduct dialogues, both with the site administrator and between users. Answer questions, start various discussions, in general, introduce full-fledged virtual communication. So it is necessary to pay attention to them and bring them into proper form.

      Here's what we'll do:

      • Full customization
      • Appearance styling
      • Comment numbering
      • Post count per user
      • Assign a status to each user
      • And other little things

      We will analyze each item separately, and at the end of the article all the functions will be fully assembled into one ready-made code.

      Customizing comments

      In WordPress, comments are displayed using the wp_list_comments function, usually in the comments.php file. And the formation of individual functions, just like the loop itself, is used from the template of the system file comment-template.php . But in rare cases, it happens that the setting can be located in the WordPress theme, functions.php file or comments.php file.

      So, if your theme does not fall into a rare case and there is a need to make your own settings, then open the functions.php file and add the following code before the ?> sign:

      If (! function_exists("my_comment")) : function my_comments($comment, $args, $depth) ( global $commentnumber; $GLOBALS["comment"] = $comment; switch ($comment->comment_type) : case " pingback" : case "trackback" : ?>

    2. ", ""); ?>
    3. id="li-comment-">
      comment_parent) $avatar_size = 39; echo get_avatar($comment, $avatar_size); /* translators: 1: comment author, 2: date and time */ printf(__("%1$s %2$s", "my_press"), sprintf(" %s", get_comment_author_link()), sprintf(" ", esc_url(get_comment_link($comment->comment_ID)), get_comment_time("c"), /* translators: 1: date, 2: time */ sprintf(__("%1$s %2$s", " my_press"), get_comment_date(), get_comment_time())))); ?>
      comment_approved == "0") : ?>
      __("Reply", "my_press"), "depth" => $depth, "max_depth" => $args["max_depth"]))); ?>
      ", ""); ?>

      Then in the comments.php file add a call function:

        "my_comments")); $commentnumber = 0; ?>

      After these manipulations, your comments will be generated according to the template function from the functions.php file of the current theme.

      Counting the comments of each user

      Using the function below, we can display the total number of posts left next to the commentator. Thus, you can observe how active the user is, and even then, statistics are not superfluous, especially in this regard.

      We open the functions.php file already familiar to us and writes the following code at the end before the sign?>:

      //count user posts function bac_comment_count_per_user() ( global $wpdb; $comment_count = $wpdb->get_var("SELECT COUNT(comment_ID) FROM ". $wpdb->comments. " WHERE comment_author_email = "" . get_comment_author_email() ." " AND comment_approved = "1" AND comment_type NOT IN ("pingback", "trackback")"); if ($comment_count == 1) ( echo " 1 Post"; ) else ( echo " " . $comment_count . " Posts "; ) )

      Now it remains to add the call function to the place you need:

      Messages will be counted based on the user's e-mail, both registered and not. Comments are taken into account only confirmed by the site administrator, but not in standby mode and deleted.

      We assign a status to each user depending on the number of comments

      This is exactly the case where statistics definitely play an important role. Since the function is built on the basis of the number of messages, and as a result, the reached number gives the user the proper status. This, in theory, is used in every forum to show the authority of the user on this resource.

      Open the functions.php file again and add the following code before the ?> sign:

      //user status function get_author_class($comment_author_email,$user_id)( global $wpdb; $adminEmail = get_option("admin_email"); $author_count = count($wpdb->get_results("SELECT comment_ID as author_count FROM $wpdb->AdminUseR "; if($author_count>=1 && $author_count<50 && $comment_author_email !==$adminEmail) echo "Прохожий"; else if($author_count>=50 && $author_count<100 && $comment_author_email !==$adminEmail) echo "Новичок"; else if($author_count>=100 && $author_count<250 && $comment_author_email !==$adminEmail) echo "Знающий"; else if($author_count>=250 && $author_count<400 && $comment_author_email !==$adminEmail) echo "Опытный"; else if($author_count>=400 &&$author_count<800 && $comment_author_email !==$adminEmail) echo "Бывалый"; else if($author_count>=800 && $author_count<1200 && $comment_author_email !==$adminEmail) echo "СуперПупер"; else if($author_count>Professor"; )

      And in the desired place we output the call function:

      comment_author_email,$comment->user_id)?>

      Explanation: the function, like the previous one, is associated with the user's e-mail. Only here the main task is not easy to count messages, but the number from and to depending on the set number. And as soon as the user reaches it, he gets a certain position. There are 7 statuses in total, plus an admin and an insignia for registered participants.

      Completely finished comment code

      Here we have come to the end of this article. Here I was not too lazy, collecting all the functions, including setting up comments in one ready-made code. I added my own appearance styles and the result was something like a mini-forum.

      Open the functions.php file and at the end before the sign?> add the following code:

      //count user posts function bac_comment_count_per_user() ( global $wpdb; $comment_count = $wpdb->get_var("SELECT COUNT(comment_ID) FROM ". $wpdb->comments. " WHERE comment_author_email = "" . get_comment_author_email() ." " AND comment_approved = "1" AND comment_type NOT IN ("pingback", "trackback")"); if ($comment_count == 1) ( echo " 1 Post"; ) else ( echo " " . $comment_count . " Posts "; ) ) //user status function get_author_class($comment_author_email,$user_id)( global $wpdb; $adminEmail = get_option("admin_email"); $author_count = count($wpdb->get_results("SELECT comment_ID as author_count FROM $ wpdb->comments WHERE comment_author_email = "$comment_author_email" ")); if($comment_author_email ==$adminEmail) echo "Admin"; if($user_id!=0 && $comment_author_email !=$adminEmail) echo "UseR"; if ($author_count>=1 && $author_count<50 && $comment_author_email !==$adminEmail) echo "Прохожий"; else if($author_count>=50 && $author_count<100 && $comment_author_email !==$adminEmail) echo "Новичок"; else if($author_count>=100 && $author_count<250 && $comment_author_email !==$adminEmail) echo "Знающий"; else if($author_count>=250 && $author_count<400 && $comment_author_email !==$adminEmail) echo "Опытный"; else if($author_count>=400 &&$author_count<800 && $comment_author_email !==$adminEmail) echo "Бывалый"; else if($author_count>=800 && $author_count<1200 && $comment_author_email !==$adminEmail) echo "СуперПупер"; else if($author_count>=1200 && $comment_author_email !==$adminEmail) echo "Professor"; ) // customizing comments if (! function_exists("wordsmall_comment")) : function wordsmall_comment($comment, $args, $depth) ( global $commentnumber; $GLOBALS["comment"] = $comment; switch ($comment- >comment_type) : case "pingback" : case "trackback" : ?>

    4. id="li-comment-">
      %s", get_comment_author_link()); ?> comment_parent)( $comment_parent_href = htmlspecialchars(get_comment_link($comment->comment_parent)); $comment_parent = get_comment($comment->comment_parent); ?> @ Answer for:comment_author;?>
      comment_parent) $avatar_size = 60; echo get_avatar($comment, $avatar_size); ?> comment_author_email,$comment->user_id)?>
      comment_approved == "0") : ?>
      ", ""); ?> __("Reply", "wallpress"), "depth" => $depth, "max_depth" => $args["max_depth"]))); ?>

      Now open the comments.php file, we find the function for calling comments in it. Looks like this:

      Change to this one:

        "wordsmall_comment")); ?>

      Final step. Open the style.css file and add the following styles at the end:

      My_commentlist( border-top:none; ) .my_commentlist .comment( padding:0 0 15px 0; border:none; ) .my_commentlist .pingback( padding:0 0 15px 0; border:none; ) .my_commentlist .comment .children ( list-style-type: none; padding:0px; margin-left:0px;/*if you need padding for tree com.set it to 15px*/ ) .my_commentlist .comment .children .comment( margin:15px 0 0 0; border: none; padding: 0; ) #comments ( background: #fff; ) #comments .my_commentlist ( margin: 10px 0; padding: 0; list-style:none; background: #ebf0f3; padding: 5px; ) #comments .my_commentlist .comment ( margin:0; padding: 0 0 10px; background: #fff; ) #comments .my_commentlist .my_comment-author ( display: inline; border-right: 1px solid #e0e0e0; width: 100px; float: left; margin : 0px 15px 10px 0; ) #comments .my_commentlist .commentmetadata ( float:left; ) #comments .my_commentlist p ( clear:none; color: #555; font: 14px arial; line-height: 23px; ) #comments .my_commentlist .comment-conte nt(margin-left: 116px; padding-right: 10px; ) #comments .my_commentlist .reply ( text-align:right; ) #comments .my_commentlist .reply a( background: #f5f5f5; border: 1px solid rgba(0, 0, 0, 0.06); border-radius: 2px; color : #515456; display: inline-block; font-size: 13px; font-weight: normal; line-height: 30px; margin-right: 15px; min-height: 30px; padding: 0 12px; text-align: center ; text-decoration: none; ) .my_commentlist .avatar( border: medium none; border-radius: 50%; float: none; margin: 5px auto; padding: 0px; display: table; ) .my_commentlist .comment-header( height: 30px; background: #DEE5EB; margin-bottom: 15px; ) .my_commentlist cite.fn( color: #444; font: bold 13px/30px arial; padding-left: 10px; ) .my_commentlist .com_date ( color: # 8ca0b5; font: normal 13px/30px arial; float: right; padding-right: 15px; ) .my_commentlist .comment-body ( overflow: hidden; position: relative; background:#fff; ) .my_commentlist .rep-authorcom ( color: #25394e; font-size: 13px; line-height: 30px; ) .my_commentlist .edit-link a ( background: none !important; border: none !important; border-radius: 0 !important; color: #999!important; display: inline-block; font-size: 11px !important; font-weight: normal; line-height: 30px; margin-right: 5px !important; min-height: 30px ; padding: 0 !important; text-align: center; text-decoration: none; ) .com_per ( border: medium none; color: #666; display: block; font-size: 11px; text-align: center; ) .vip1, .vip, .vp, .vip2, .vip3, .vip4, .vip5, .vip6, .vip7 ( border: medium none; font: bold 13px arial; display: block; text-align: center; margin- bottom: 5px; text-decoration: none; ) .vp (color: #e82e24;) .vip1 (color: #348be8;) .vip2 (color: #BE005E;) .vip3 (color: #2e517e;) .vip4 ( color: #658a18;) .vip5 (color: #00A56D;) .vip6 (color: #e35d28;) .vip7 (color: #99A400;) .vip (color: #4c5176; font-size: 11px; margin: 0 ;)

      The code is fully working and does not cause errors, but minor changes in CSS styles may be needed.

      WordPress has several types of content such as posts, pages, comments. WordPress is a very flexible platform that allows you to customize the main types of content to suit your site. You can change the look and feel. In this tutorial, we'll show you how to change the behavior and appearance of comments on a WordPress site.

      Step 1. Understanding the function comment_form and its arguments

      Consider the WordPress comment_form function. It is responsible for displaying the comment form that is displayed on the page or post. The call to this function can mainly be found in the file comments.php in the theme folder. This file is included in various places, for example, in files single.php And page.php, directly or through a call to the comments_template function.

      A description of the feature can be found in the WordPress codex.

      If you use the comment_form function to render a form, it will be rendered using the default parameters and will contain fields such as name, email (both fields are required), website, and comment content. In the default Twenty Eleven theme, the form will look like this.

      Some important arguments to the comment_form function:

      • fields - with its help you can control the output of fields in the comment form.
      • comment_notes_before and comment_notes_after are used to display information before and after the form.
      • title_reply - used to change the title of the response, which defaults to 'Leave a Reply'.
      • label_submit - used to change the text on the comment submit button.

      Step 2. Customize the comment form using the function comment_form

      Now let's customize our comment form by passing arguments to the comment_form function.

      In case we need to customize the fields in the comment form, we need to pass a list of them to the comment_form function. By default, the function uses the following list of fields:

      $fields = array("author" => "

      " . "" . ($req ? " *" : "") . "

      ", "email" => " ", "url" => "

      " . "

      ",);

      If we need to remove a field, say website , we just need to exclude it from the array and pass the array to the comment_form function.

      $commenter = wp_get_current_commenter(); $req = get_option("require_name_email"); $aria_req = ($req ? " aria-required="true"" : ""); $fields = array("author" => "

      " . "" . ($req ? " *" : "") . "

      ", "email" => " ",); $comments_args = array("fields" => $fields); comment_form($comments_args);

      In addition, we will also change the name of the form to ‘Please give us your valuable comment’ and the label on the button to ‘Send My Comment’.

      To complete the task, we pass the following arguments to the comment_form function:

      $commenter = wp_get_current_commenter(); $req = get_option("require_name_email"); $aria_req = ($req ? " aria-required="true"" : ""); $fields = array("author" => "

      " . "" . ($req ? " *" : "") . "

      ", "email" => " ",); $comments_args = array("fields" => $fields, "title_reply"=>"Please give us your valuable comment", "label_submit" => "Send My Comment"); comment_form($comments_args);

      The comment form will now look like this:

      Step 3 Removing fields from a form with a hook

      Also the WordPress comment form can be modified with hooks and filters. This setup can be especially useful when working with a plugin when you need to tweak a few elements but not change the theme files. Filter to add or remove form fields - ' comment_form_default_fields '

      Let's remove the URL address field using a filter. The above code can be used in a plugin or in a file functions.php active topic.

      Function remove_comment_fields($fields) ( unset($fields["url"]); return $fields; ) add_filter("comment_form_default_fields","remove_comment_fields");

      Step 4: Adding Data to the Comment Form with a Hook

      We can add fields to the form using the ' comment_form_default_fields ' filter. Let's add the author's age field using a filter and save this field with additional data and display it in the comments.

      Add a field like this:

      Function add_comment_fields($fields) ( $fields["age"] = "

      " . "

      "; return $fields; ) add_filter("comment_form_default_fields","add_comment_fields");

      #respond .comment-form-author label, #respond .comment-form-email label, #respond .comment-form-url label, #respond .comment-form-age label, #respond .comment-form-comment label ( background: #eee; -webkit-box-shadow: 1px 2px 2px rgba(204,204,204,0.8); -moz-box-shadow: 1px 2px 2px rgba(204,204,204,0.8); box-shadow: 1px 2px 2px rgba(204,204,204, 0.8); color: #555; display: inline-block; font-size: 13px; left: 4px; min-width: 60px; padding: 4px 10px; position: relative; top: 40px; z-index: 1; )

      Now our comment form will look like this:

      Age is now stored as additional information. You need to use a hook in ' comment_post ':

      Function add_comment_meta_values($comment_id) ( if(isset($_POST["age"])) ( $age = wp_filter_nohtml_kses($_POST["age"]); add_comment_meta($comment_id, "age", $age, false); ) ) add_action("comment_post", "add_comment_meta_values", 1);

      Once the data is saved, it can be displayed in a comment like this:

      comment_ID, "age", true); ?>

      Step 5 Setting comments for specific post types

      Sometimes you want to use fields in comments only for certain types of posts. Let's change the code to display the age field only for the record type book :

      Function add_comment_fields($fields) ( if(is_singular("books")) ( $fields["age"] = "

      " . "

      "; ) return $fields; ) add_filter("comment_form_default_fields","add_comment_fields");

      Step 6. Create a return function for displaying comments

      The wp_list_comments function is used to display comments in posts. The WordPress codex describes the feature in detail.

      wp_list_comments has a ' callback ' argument which can be used to define a function that is called when a comment is displayed.

      In the Twenty Eleven theme in the file comments.php you can find the line:

      wp_list_comments(array("callback" => "twentyeleven_comment"));

      Let's change it to:

      wp_list_comments(array("callback" => "my_comments_callback"));

      The my_comments_callback function will be called for every post.

      Step 7 Styling the Comments

      Now we will change the style of the comment a bit. We will simply display the content of the post and the age field that we added earlier. We will also change the background color for comments.

      Function code ‘ my_comments_callback ’:

      Function my_comments_callback($comment, $args, $depth) ( $GLOBALS["comment"] = $comment; ?>

    5. id="li-comment-">

      comment_ID, "age", true); ?>

      __("Reply ↓", "twentyeleven"), "depth" => $depth, "max_depth" => $args["max_depth"]))); ?>
    6. Change the background color like this:

      Commentlist > li.comment ( background: #99ccff; border: 3px solid #ddd; -moz-border-radius: 3px; border-radius: 3px; margin: 0 0 1.625em; padding: 1.625em; position: relative; )

  • Share with friends or save for yourself:

    Loading...