Adds meta data field to a comment.
Parameters
$comment_idintrequired- Comment ID.
$meta_keystringrequired- Metadata name.
$meta_valuemixedrequired- 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.
- false is stored and retrieved as an empty string (
$uniquebooloptional- Whether the same key should not be added.
Default:
false
Source
function add_comment_meta( $comment_id, $meta_key, $meta_value, $unique = false ) {
return add_metadata( 'comment', $comment_id, $meta_key, $meta_value, $unique );
}
Changelog
| Version | Description |
|---|---|
| 2.9.0 | Introduced. |
// Adding comment meta data
$comment_id = 123;
$meta_key = ‘user_rating’;
$meta_value = 5;
add_comment_meta( $comment_id, $meta_key, $meta_value );
$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.
This will add a custom field named user_rating with the value 5 to the comment with ID 123.
Basic Example
Add a custom posted value to every new comment