Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use comment_class filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
October 12, 2022
5 minutes read

comment_class filter

Filters the returned CSS classes for the current comment.

To use the comment_class filter, first you have to register it using add_filter. You can write this code into the functions.php of your activated theme or in a custom WordPress Plugin.

We at WePlugins always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.

In the below live example, we have defined a function modify_comment_class_defaults which takes 5 parameters and we registered using add_filter. The first parameter comment_class is the name of the hook, the second parameter modify_comment_class_defaults is the name of the function which needs to be called, the third parameter is the priority of calling the hook if the same hook is used multiple times and the last parameter is the number of arguments (if any) to be passed in the registered function.

Sometimes, you have to remove a registered hook so you can use remove_filter to remove the comment_class filter.

Parameters

Below are the 5 parameters required to use this hook.

  • $classes : (string[]) An array of comment classes.
  • $css_class : (string[]) An array of additional classes added to the list.
  • $comment_id : (string) The comment ID as a numeric string.
  • $comment : (WP_Comment) The comment object.
  • $post_id : (int|WP_Post) The post ID or WP_Post object.

Live Examples

Example 1: Basic Usage

Below is an example of how you can use this hook.

    function weplugins_modify_comment_class_defaults($classes, $css_class, $comment_id, $comment, $post_id) { 
        // Update the $classes variable according to your website requirements and return this variable. You can modify the $classes variable conditionally too if you want.
        return $classes; 
    }
    // add the filter
    add_filter( "comment_class", "weplugins_modify_comment_class_defaults", 10, 5 );
    

Example 2: Conditional Class Modification

In this example, we will modify the comment classes conditionally based on the comment author’s email.

    function weplugins_modify_comment_class_by_email($classes, $css_class, $comment_id, $comment, $post_id) {
        if ($comment->comment_author_email == 'example@example.com') {
            $classes[] = 'special-comment';
        }
        return $classes;
    }
    add_filter( "comment_class", "weplugins_modify_comment_class_by_email", 10, 5 );
    

Example 3: Removing a Hook Callback

To remove a hook callback, use the example below.

    remove_filter( "comment_class", "weplugins_modify_comment_class_defaults", 10, 5 );
    

Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.

Access Premium WordPress Plugins

Contact Us

If you need any customization or have any trouble using this hook, please contact us.

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.