I'm trying to add a CSS class to a comment and everything seems to work if the comment id was hard coded in. However it's not working because I don't know how to get the comment id and then from there, get the comment meta stored in the database.

in wp_commentmeta I have the following data:





I then have this code using a filter hook to add the class to the list of comment classes. However, this is in a standalone plugin in the /wp-content/plugins folder and I need to loop through all the comments for that post.

function add_comments_class( $classes, $class, $comment_ID, $comment, $post_id ) {

    $meta_data = get_comment_meta( $comment_ID, 'score', true );

    $meta_data = round((float)$meta_data * 100 );

    $classes[] = $meta_data;

    return $classes;

} add_filter( 'comment_class', 'add_comments_class' );

I've looked at the Codex but that doesn't seem to identify how to get the ID when viewing a post etc.

The variables in the function were taken from this answer here although it doesn't explain why those specific variables are used or where they are from.

2 Answers 2


I found out that you need to use the $comment variable within your function which should then allow you to search the database properly.

Using the global variable scope you can select the main comment variable.

global $comment;

And to get the comment id, you can use:

global $comment;
echo $comment->comment_ID;
  • I think it would be better to reduce the direct use of globals, specially when the comment ID is available within the callback.
    – birgire
    Commented Mar 19, 2017 at 14:33
  • How could I use it within the callback? I couldn't find any info on this. I might have been searching for the wrong thing though.
    – user111400
    Commented Mar 19, 2017 at 14:41
  • Did you try the changes I suggested regarding the priority and number of input numbers in the add_filter() part?
    – birgire
    Commented Mar 19, 2017 at 15:46
  • Yes, it doesn't work. I checked another plugin to see how someone else did it and they have done it the same way.
    – user111400
    Commented Mar 19, 2017 at 16:17
  • it works well for me, if you echo the $comment_ID within it, what do you get when you view the HTML source? How does your current code snippet look like?
    – birgire
    Commented Mar 19, 2017 at 16:52

It looks like you forgot to add the priority and number of input arguments, so change

add_filter( 'comment_class', 'add_comments_class' );


add_filter( 'comment_class', 'add_comments_class', 10, 5 );

where 10 is the priority and 5 is the number of input arguments passed on to your callback.

You can see that I specify the priority and number input arguments for the callback in my previous answer that you linked to.

Also consider using a prefix to your callbacks, like wpse_260620_add_comments_class(), or use a namespace, to avoid a possible name collision.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.