I am looking to add some meta data to my Woo Commerce product reviews.
My plugin shows which product variant the customer is reviewing, and I wanted to add that information into each of the customer's reviews (comments), with a small thumbnail img of the product varient to fancy things up a bit as well.
But I am having trouble finding a filter / hook to tap into.
I have tried this one...
add_filter( 'comment_text', function( string $comment_text ) {
$comment_text = '<p>Comment text injection</p>' . $comment_text;
return $comment_text;
});
It works, but the problem is , it does not provide much context... I need the comment ID so I can grab some meta data about the comment.
The documentation says it is possible to for this filter to have a WP_Comment obj passed with the filter... but that doesn't happen in my case.
https://developer.wordpress.org/reference/hooks/comment_text
Any suggestions on hooks/filters available to use - I don't really want to have to start hacking the comments template.
The comment_text
filter hook allow 3 function arguments (so you missed 2 of them):
$comment_text
(string), the main filtered argument$comment
(object), the current WP_Comment
Object instance$args
(array), an array of argumentsSo in this hooked function, here is an example targeting Order notes for example:
add_filter( 'comment_text', 'customizing_comment_text', 20, 3 );
function customizing_comment_text( $comment_text, $comment, $args ) {
if( $comment->comment_type === 'review' ) {
$comment_text = '<p>Comment text injection</p>' . $comment_text;
}
return $comment_text;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and work.
To get specific comment meta data you will use the function get_comment_meta()
like:
$meta_value = get_comment_meta( $comment->comment_ID, 'your_meta_key', true );
To Add specific comment meta data you will use the function add_comment_meta()
like:
add_comment_meta( $comment_id, 'your_meta_key', $meta_value, $unique );