Setting up comments in WordPress. Allowing users to leave anonymous comments in WordPress How to completely remove the Name and Email fields from the comment form

💖 Do you like it? Share the link with your friends

WordPress has several types of content such as posts, pages, comments. WordPress is a very flexible platform that allows you to customize basic content to suit your site. Can be changed appearance and functionality. In this tutorial we will show you how to change the behavior and appearance of comments on a WordPress site.

Step 1. Understanding the comment_form function and its arguments

Let's take a look at the WordPress comment_form function. It is responsible for displaying the comment form that appears on the page or post. The call to this function can mainly be found in the comments.php file in the theme folder. This file included in various places, such as in the single.php and page.php files, directly or through a call to the comments_template function.

Description of the function can be found in the WordPress codex.

If you use the comment_form function to display 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 - you can use it to control the display of fields in the comment form.
  • comment_notes_before and comment_notes_after - used to display information before and after the form.
  • title_reply - used to change the title of the reply, 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 comment_form function

Now let's set up 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" => "

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

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

" . __("Website") . "" . "

",);

If we need to remove a field, for example website , we simply 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" => "

" . "" . __("Name") . " " . ($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 inscription 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" => "

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

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

Now the comment form will look like this:

Step 3. Removing fields from a form using a hook

Also form WordPress comments can be modified using hooks and filters. This setting can be especially useful when working with a plugin, when you need to customize several elements, but not change the theme files. Filter for adding or removing fields from the form - ‘ comment_form_default_fields ‘

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

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

Step 4. Add data to the comment form using 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 comment.

Add a field like this:

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

" . __("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:

Now the age is stored as Additional Information. You need to use the 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 output as a comment like this:

Step 5. Setting up comments for specific post types

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

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

" . __("Age") . "" . "

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

Step 6. Create a callback function to display comments

The wp_list_comments function is used to display comments on posts. In the WordPress code the function is described in detail.

wp_list_comments has a ' callback ' argument in which you can define a function that is called when a comment is displayed.

In the Twenty Eleven theme in the comments.php file 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 each post.

Step 7: Styling Comments

Now we'll change the comment style a little. We will simply display the contents 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; ?>

">

tell friends