Using WordPress ‘get_comment_id_fields()’ PHP function

The get_comment_id_fields WordPress PHP function retrieves hidden input HTML for replying to comments.

Usage

get_comment_id_fields( $post_id )

Custom Example:

Input:

echo get_comment_id_fields( 42 );

Output:

<input type="hidden" name="comment_post_ID" value="42" id="comment_post_ID">
<input type="hidden" name="comment_parent" id="comment_parent" value="0">

Parameters

  • $post_id (int|WP_Post|null) Optional: The post the comment is being displayed for. Defaults to the current global post. Default: null

More information

See WordPress Developer Resources: get_comment_id_fields

Examples

Display comment ID fields for a specific post

Retrieve the comment ID fields for a specific post with ID 42:

$post_id = 42;
echo get_comment_id_fields( $post_id );

Display comment ID fields for the current post

Retrieve the comment ID fields for the current global post:

echo get_comment_id_fields();

Add the comment ID fields to a custom comment form

Create a custom comment form and include the comment ID fields:

function my_custom_comment_form() {
    $post_id = get_the_ID();
    ?>
    <form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">
        <div>
            <label for="author">Name:</label>
            <input type="text" name="author" id="author" value="<?php echo esc_attr($comment_author); ?>" />
        </div>
        <div>
            <label for="comment">Comment:</label>
            <textarea name="comment" id="comment"></textarea>
        </div>
        <?php echo get_comment_id_fields( $post_id ); ?>
        <div>
            <input type="submit" name="submit" value="Submit Comment" />
        </div>
    </form>
    <?php
}

Display comment ID fields for a custom post object

Retrieve the comment ID fields for a custom post object:

$post = get_post( 42 );
echo get_comment_id_fields( $post );

Manually create comment ID fields for a specific post

Manually create the comment ID fields for a specific post with ID 42:

$post_id = 42;
?>
<input type="hidden" name="comment_post_ID" value="<?php echo $post_id; ?>" id="comment_post_ID">
<input type="hidden" name="comment_parent" id="comment_parent" value="0">
<?php