Home WordPress Comment Functions comment_id_fields() 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.
comment_id_fields();
comment_id_fields( 42 ); Call What 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 comment_id_fields( int|WP_Post|null $post = null ): void Parameters Parameter Description $postOptional. A post ID or WP_Post object. Default is the current post.
Return Value Return Value Description void Nothing is returned. The function prints two hidden inputs.
WordPress Examples Three short theme examples. Each one has a sample output.
1. In a custom form 2. For a given post 3. Return the fields Example 1 — In a custom form Add the hidden fields to a hand-made form.
<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> <form action="https://example.com/wp-comments-post.php" method="post">
<textarea name="comment"></textarea>
<input type='hidden' name='comment_post_ID' value='42' id='comment_post_ID' />
<input type='hidden' name='comment_parent' id='comment_parent' value='0' />
<button type="submit">Send</button>
</form> How It Works The form posts to wp-comments-post.php. comment_id_fields() adds the post ID and the parent ID.comment_parent is 0 for a top-level comment.Example 2 — For a given post Print the fields for a post by its ID.
<?php comment_id_fields( 42 ); ?> <input type='hidden' name='comment_post_ID' value='42' id='comment_post_ID' />
<input type='hidden' name='comment_parent' id='comment_parent' value='0' /> How It Works The argument 42 is the post ID. Use this when the form is not in the main loop. The parent value is still 0. Example 3 — Return the fields Use get_comment_id_fields() to keep the HTML in a variable.
<?php $fields = get_comment_id_fields(); ?>
<?php echo $fields; ?> <input type='hidden' name='comment_post_ID' value='42' id='comment_post_ID' />
<input type='hidden' name='comment_parent' id='comment_parent' value='0' /> How It Works get_comment_id_fields() returns the HTML.You can echo it where you need it. WordPress has already escaped the values. Frequently Asked Questions What does comment_id_fields() print? Two hidden inputs: comment_post_ID and comment_parent.
Do I need it with comment_form()? No. comment_form() already includes the fields.
What is comment_parent? It is the ID of the comment being replied to. It is 0 for a new top-level comment.
Where do I place it? Inside the form tag of your custom comment form.
How do I get the fields as a string? Use get_comment_id_fields().
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion