Add class to Reply button in Comments area

Under each comment there is a link called ‘Reply’. I want to add a class to this link. It’s default class is comment-reply-link. How can I do so? I am creating my own theme and I don’t want to manually edit files inside the wp-includes directory.

Related posts

Leave a Reply

5 comments

  1. I know this is quite an old post and maybe this could help someone.

    You can replace the class of an element using add_filter();

    Here’s an example:

    // filter to replace class on reply link
    
    //           class name             function name
    add_filter('comment_reply_link', 'replace_reply_link_class');
    
    
    function replace_reply_link_class($class){
        $class = str_replace("class='comment-reply-link", "class='reply", $class);
        return $class;
    }
    

    With this you should be able to replace the class in your themes.

    I hope this can be of any help for someone 🙂

  2. Quite old post, but while searching I came here, maybe someone else will find it useful.

    The only solution I found is to completely rebuilt the button that comment_reply_link returns.

    I first made two vars reconstructing href and onclick attributes for the reply button:

    $reply_href = wp_make_link_relative(
        get_permalink( $comment->comment_post_ID ) 
        ) 
        . '?replytocom=' . $comment->comment_ID . '#respond';
    
    $reply_onclick = 'return addComment.moveForm("comment-' 
        . $comment->comment_ID 
        . '", "' 
        . $comment->comment_ID 
        . '", "respond", "' 
        . $comment->comment_post_ID 
        . '")';
    

    And then output the whole reply block, adding your custom classes:

    <div class="reply">
        <a class="comment-reply-link custom-class" href="<?php 
            echo $reply_href; 
            ?>" onclick="<?php 
            echo $reply_onclick; 
            ?>"><?php 
            _e('Reply', 'yourthemename'); 
        ?></a>
    </div>
    
  3. This might not be an elegant solution but it gets the job done. Do it with jQuery. Basically find the element and add the class you need. WordPress totally didn’t make this easy to customize:

    jQuery('a#comment-reply-link').addClass('mycustomcssclass');
    
  4. Also something to double check (as I overlooked this for about half an hour)…

    Make sure under: Settings > Discussion

    This checkbox is checked:

    Enable threaded (nested) comments levels deep

    Otherwise the reply button will not show up at all