avatar Deluxe Blog Tips About Projects

Displaying comment form in WordPress

Displaying comment form in WordPress is very simple. WordPress provides a function comment_form() that helps you doing that in a few seconds. In this post, we'll see how to implement this function in your theme.

To use the function comment_form() to display comment form in your WordPress website, just open the comments.php file of your theme, find the code to display comments, it looks like this:

<div id="respond">
    <h3><?php comment_form_title( 'Leave a Reply', 'Leave a Reply to %s' ); ?></h3>

    <div id="cancel-comment-reply"><?php cancel_comment_reply_link() ?></div>

Add this function to that file where you want to display the comment form:

<?php comment_form(); ?>

This function has 2 parameters, all of them are optional:

  • $args: Options for strings, fields, etc in the form
  • $post: Post object or ID to generate the form for, uses the current post if not passed

The details can be found at the WordPress documentation.

You can pass the full list of parameters to the function. However, there is another way to customize the form fields using the comment_form_default_fields filter.

For example, if you want to hide the comment URL field, just put the following code into functions.php file:

add_filter( 'comment_form_default_fields', function ( $fields ) {
    unset( $fields['url'] );
    return $fields;
} );

Also, form fields are also individually passed through a filter of the form comment_form_field_$name where $name is the key used in the array of fields. So, we can hide the URL field in another way like this:

add_filter( 'comment_form_field_url', fn() => '' );

This is just an example of using the filter for the comment form. If you're not familiar with coding, you can use my Falcon plugin to do that for you. Anyway, you can use these hooks to customize any fields of the form.

🔥 HOT: Interested in boosting your WordPress SEO? My Slim SEO plugin is a super lighweight and automated plugin that handles most the hard work for you: meta tags, sitemap, redirection, schema & link building.

👉 Have a look and you will love it: wpslimseo.com