Using WordPress ‘comment_form_title()’ PHP function

The comment_form_title() WordPress PHP function displays text based on the comment reply status. This function particularly affects users who have JavaScript disabled. It’s used to show different text when replying to a comment and when not replying to a comment. If desired, the author’s name can be made into a link to their comment. By default, the comment form is displayed for the current global post.

Usage

The comment_form_title() function can be used like this:

comment_form_title('Your text here', 'Replying to %s');

The first argument is the text to display when not replying to a comment. The second argument is the text to display when replying to a comment. Here, “%s” will be replaced by the author of the comment being replied to.

Parameters

  • $no_reply_text (string|false): Optional. Text to display when not replying to a comment. Default is false.
  • $reply_text (string|false): Optional. Text to display when replying to a comment. Accepts “%s” for the author of the comment being replied to. Default is false.
  • $link_to_parent (bool): Optional. Boolean to control making the author’s name a link to their comment. Default is true.
  • $post (int|WP_Post|null): Optional. The post that the comment form is being displayed for. Defaults to the current global post. Default is null.

More information

See WordPress Developer Resources: comment_form_title()

Examples

Displaying Default Title

In this example, the function will display the default text.

<h3>
  comment_form_title();
</h3>

Customizing Text

Here, custom text is displayed when not replying to a comment and when replying to a comment.

<h3>
  comment_form_title('Leave a Comment', 'Leave a Comment to %s');
</h3>

In this instance, the author’s name won’t be a link to their comment.

<h3>
  comment_form_title('Leave a Comment', 'Leave a Comment to %s', false);
</h3>

Specifying a Post

Here, the comment form title is displayed for a specific post.

<h3>
  comment_form_title('Leave a Comment', 'Leave a Comment to %s', true, $post_id);
</h3>

Using Translation Functionality

In this example, we’re using the __() function to make the text translatable.

<h3>
  comment_form_title( __('Leave a Comment', 'textdomain'), __('Leave a Comment to %s', 'textdomain') );
</h3>

In this case, ‘textdomain’ should be replaced with your theme’s text domain.