Title: add_comment_meta
Published: April 25, 2014
Last modified: February 24, 2026

---

# add_comment_meta( int $comment_id, string $meta_key, mixed $meta_value, bool $unique = false ): int|false

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/add_comment_meta/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/add_comment_meta/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/add_comment_meta/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/add_comment_meta/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/add_comment_meta/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/add_comment_meta/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/add_comment_meta/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/add_comment_meta/?output_format=md#wp--skip-link--target)

Adds meta data field to a comment.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/add_comment_meta/?output_format=md#description)󠁿

For historical reasons both the meta key and the meta value are expected to be “
slashed” (slashes escaped) on input.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/add_comment_meta/?output_format=md#parameters)󠁿

 `$comment_id`intrequired

Comment ID.

`$meta_key`stringrequired

Metadata name.

`$meta_value`mixedrequired

Metadata value. Arrays and objects are stored as serialized data and will be returned
as the same type when retrieved. Other data types will be stored as strings in the
database:

 * false is stored and retrieved as an empty string (`''`)
 * true is stored and retrieved as `'1'`
 * numbers (both integer and float) are stored and retrieved as strings Must be 
   serializable if non-scalar.

`$unique`booloptional

Whether the same key should not be added.

Default:`false`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/add_comment_meta/?output_format=md#return)󠁿

 int|false Meta ID on success, false on failure.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/add_comment_meta/?output_format=md#source)󠁿

    ```php
    function add_comment_meta( $comment_id, $meta_key, $meta_value, $unique = false ) {
    	return add_metadata( 'comment', $comment_id, $meta_key, $meta_value, $unique );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/comment.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/comment.php#L468)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/comment.php#L468-L470)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/add_comment_meta/?output_format=md#related)󠁿

| Uses | Description | 
| [add_metadata()](https://developer.wordpress.org/reference/functions/add_metadata/)`wp-includes/meta.php` |

Adds metadata for the specified object.

  |

| Used by | Description | 
| [wp_spam_comment()](https://developer.wordpress.org/reference/functions/wp_spam_comment/)`wp-includes/comment.php` |

Marks a comment as Spam.

  | 
| [wp_insert_comment()](https://developer.wordpress.org/reference/functions/wp_insert_comment/)`wp-includes/comment.php` |

Inserts a comment into the database.

  | 
| [wp_trash_comment()](https://developer.wordpress.org/reference/functions/wp_trash_comment/)`wp-includes/comment.php` |

Moves a comment to the Trash

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/add_comment_meta/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.9.0](https://developer.wordpress.org/reference/since/2.9.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/add_comment_meta/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 3 content](https://developer.wordpress.org/reference/functions/add_comment_meta/?output_format=md#comment-content-7250)
 2.    [Hitesh Patel](https://profiles.wordpress.org/patelhitesh/)  [  1 year ago  ](https://developer.wordpress.org/reference/functions/add_comment_meta/#comment-7250)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_comment_meta%2F%23comment-7250)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_comment_meta%2F%23comment-7250)
 4.  // Adding comment meta data
      $comment_id = 123; $meta_key = ‘user_rating’; $meta_value
     = 5;
 5.  add_comment_meta( $comment_id, $meta_key, $meta_value );
 6.  $comment_id: The ID of the comment you want to attach the metadata to.
      $meta_key:
     A unique key that identifies the type of metadata you are storing. $meta_value:
     The value associated with the metadata. In this case, a numeric rating of 5.
 7.  This will add a custom field named user_rating with the value 5 to the comment
     with ID 123.
 8.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_comment_meta%2F%3Freplytocom%3D7250%23feedback-editor-7250)
 9.   [Skip to note 4 content](https://developer.wordpress.org/reference/functions/add_comment_meta/?output_format=md#comment-content-1140)
 10.   [Codex](https://profiles.wordpress.org/codex/)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/add_comment_meta/#comment-1140)
 11. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_comment_meta%2F%23comment-1140)
     Vote results for this note: -6[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_comment_meta%2F%23comment-1140)
 12. **Basic Example**
 13. Add a custom posted value to every new comment
 14.     ```php
         <?php
         function add_custom_comment_field( $comment_id ) {
     
            add_comment_meta( $comment_id, 'my_custom_comment_field', $_POST['my_custom_comment_field'] );
         }
         add_action( 'comment_post', 'add_custom_comment_field' );
         ?>
         ```
     
 15.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_comment_meta%2F%3Freplytocom%3D1140%23feedback-editor-1140)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fadd_comment_meta%2F)
before being able to contribute a note or feedback.