# Optional Title Field for Comments

> Adds an optional "Title" field to the comment form and shows it as a heading above the comment, handy for reviews and longer discussions.

- Type: PHP
- Runs: Run everywhere
- Category: [Forms](https://wpide.com/library/category/forms/)
- Tags: comments, fields, reviews
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/form-comment-title-field/

## Code

```php
add_filter( 'comment_form_field_comment', function ( $field ) {
    return '<p class="comment-form-title"><label for="wpide-comment-title">Title (optional)</label><input type="text" id="wpide-comment-title" name="wpide_comment_title" maxlength="100"></p>' . $field;
} );

add_action( 'comment_post', function ( $comment_id ) {
    $title = isset( $_POST['wpide_comment_title'] ) ? sanitize_text_field( wp_unslash( $_POST['wpide_comment_title'] ) ) : '';

    if ( '' !== $title ) {
        add_comment_meta( $comment_id, 'wpide_comment_title', mb_substr( $title, 0, 100 ), true );
    }
} );

add_filter( 'comment_text', function ( $text, $comment = null ) {
    if ( ! $comment || is_admin() ) {
        return $text;
    }

    $title = (string) get_comment_meta( $comment->comment_ID, 'wpide_comment_title', true );

    return '' !== $title ? '<p class="comment-title"><strong>' . esc_html( $title ) . '</strong></p>' . $text : $text;
}, 9, 2 );
```

## Notes

Titles are limited to 100 characters and stored as comment meta named wpide_comment_title.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Optional Title Field for Comments" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
