WordPress comment_id_fields() Function

Beginner
4 min read
Updated: Nov 2026
3 examples
Template tag

Definition

In WordPress, comment_id_fields() prints two hidden inputs: comment_post_ID and comment_parent.

They tell WordPress which post the comment belongs to, and which comment it replies to. You need them only when you build a custom comment form by hand.

Syntax

Call it inside your form tag. Pass a post if it is not the current post.

WordPress
comment_id_fields();
comment_id_fields( 42 );
CallWhat it does
comment_id_fields()Prints the hidden fields for the current post.
comment_id_fields( 42 )Prints the hidden fields for the post with ID 42.
get_comment_id_fields()Returns the fields as a string.

If you use comment_form(), this is done for you. Do not call it twice.

Full declaration
WordPress
comment_id_fields( int|WP_Post|null $post = null ): void

Parameters

ParameterDescription
$postOptional. A post ID or WP_Post object. Default is the current post.

Return Value

Return ValueDescription
voidNothing is returned. The function prints two hidden inputs.

WordPress Examples

Three short theme examples. Each one has a sample output.

Example 1 — In a custom form

Add the hidden fields to a hand-made form.

WordPress
<form action="<?php echo esc_url( site_url( '/wp-comments-post.php' ) ); ?>" method="post">
  <textarea name="comment"></textarea>
  <?php comment_id_fields(); ?>
  <button type="submit">Send</button>
</form>

How It Works

  1. The form posts to wp-comments-post.php.
  2. comment_id_fields() adds the post ID and the parent ID.
  3. comment_parent is 0 for a top-level comment.

Example 2 — For a given post

Print the fields for a post by its ID.

WordPress
<?php comment_id_fields( 42 ); ?>

How It Works

  1. The argument 42 is the post ID.
  2. Use this when the form is not in the main loop.
  3. The parent value is still 0.

Example 3 — Return the fields

Use get_comment_id_fields() to keep the HTML in a variable.

WordPress
<?php $fields = get_comment_id_fields(); ?>
<?php echo $fields; ?>

How It Works

  1. get_comment_id_fields() returns the HTML.
  2. You can echo it where you need it.
  3. WordPress has already escaped the values.

Frequently Asked Questions

Two hidden inputs: comment_post_ID and comment_parent.
No. comment_form() already includes the fields.
It is the ID of the comment being replied to. It is 0 for a new top-level comment.
Inside the form tag of your custom comment form.
Use get_comment_id_fields().

Next: comment_reply_link()

Print the Reply link that lets a visitor answer one comment.

comment_reply_link() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful