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.
5 Answers
In your comments.php template file use wp_list_comments
and set the parameter callback
to your defined function that will generate the template. Inside the function you can style the comment reply link.
-
I am using the callback already. But the callback uses comment_reply_link which doesn't appear to offer a facility for modifying classes. Mar 22, 2011 at 22:43
-
@mrtsherman - In that case because I also cannot find a way to added another class. You can enclose the
comment_reply_link
with a<span> or <div>
with a class and address the link as.class a
. Mar 22, 2011 at 22:50 -
Thank-you for the links. I see under the further reading section them using add_filter to add CSS classes. I understand the syntax of the function, but how I can hook into it for my use case I have no idea. What $tag would I use? Mar 22, 2011 at 22:51
-
thank-you for the idea. Unfortunately I am really trying to add the class to the actual link. This has to do with using a stylesheet hosted from another server and this Wordpress theme is extending their templates. If nobody else can come up with a better solution I will mark this as best answer. Mar 22, 2011 at 22:53
-
I ended up using comment_form and the comment_notes_after parameter to add my own button. I hid the default button using CSS and then click it using javascript. Ugly I know, but WP seems to have made it very difficult to fundamentally change things. Mar 23, 2011 at 22:05
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>
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');
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