In a typical WordPress theme you output the entire list of comments for a Post/Page by using the function wp_list_comments(). This doesn’t offer much by the way of customizing what HTML markup gets generated for that comment list. To write your own markup for the comment list, you can use a callback function as a parameter in wp_list_comments(), so it’s just as nicely abstracted.
In functions.php
<?php
function my_custom_comments($comment, $args, $depth) {
$GLOBALS['comment'] = $comment; ?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
<?php if ($comment->comment_approved == '0') : ?>
<em><?php _e('Your comment is awaiting moderation.') ?></em>
<?php endif; ?>
// Comments markup code here, e.g. functions like comment_text();
}
?>
In comments.php
<?php
wp_list_comments("callback=my_custom_comments");
?>
Pasted as is, this breaks my functions file.
God, I swear there’s like NOTHING out there that thoroughly explains comments for WP.
I completely agree. I’m building my first theme and I can’t find anything on the web that gives a great “tutorial-like” breakdown of customizing the comments section. SOOOO frustrating.
God, i couldn’t agree more..
First time i want to use custom comments made by me and cant find anything anywhere?!
Why doesnt someone share something really helpful?
Good theory. I like it. Appreciate your posting
thanks.. very good
The reason is breaks is because the php tag isn’t opened at the end with the closing bracket. It should look like the above code.
This was a great help. With basic php knowledge and adding a few php tags your code works a treat. I recommend playing with these functions to get the desired result:
cancel_comment_reply_link(), comment_author(), comment_author_email(), comment_author_email_link(), comment_author_IP(), comment_author_link(), comment_author_rss(), comment_author_url(), comment_author_url_link(), comment_class(), comment_date(), comment_excerpt(), comment_form_title(), comment_form(), comment_ID(), comment_id_fields(), comment_reply_link(), comment_text(), comment_text_rss(), comment_time(), comment_type(), comments_link, comments_number(), comments_popup_link(), comments_popup_script(), comments_rss_link(), get_avatar(), next_comments_link(), paginate_comments_links(), permalink_comments_rss(), previous_comments_link(), wp_list_comments()
Great addition to the post. Thank you! :)
This code is extremely useful if you’re already familiar with PHP, but it’s not complete, and it does break the functions.php file. However, I played around with it and came up with a conversion that can be pasted directly into the functions.php.
View the code at PasteBin
It’s tested, but not thoroughly, so use at your own risk. But if you use the list of functions that Ralph posted, you should be able to create your custom comments.
Make sure to use the same
that Chris posted in your comments.php file.
Thank you… have been looking for this for a couple days.
I agree it was difficult to find info on doing custom styling for wordpress comments. Here is the function I finally used — hope this helps! … It allows a lot of customization. I also included a function for changing the default gravatar image if you want to use your own image instead of “mystery man” or the other gravatar defaults. Link to my gist below.
Here’s the gist, if you wanna download the file :)
https://gist.github.com/4192115
I would be very interested to learn from this, are you planning to do a tutorial regarding this snippet? thank you in advance if so :)
Thanks a lot Rowe for your much appreciated snippet of php code. This is definetley the kind of extra that adds value to one’s WordPress website, let alone CSS tips and tricks ;-)
I think you forgot to add the closing ” tag before closing the function.
Looks like your HTML got gobbled up there, but if you meant
<li>
tag, you are specifically supposed to leave that off. WordPress will close them as needed. It needs you to do that for threading comments.Thanks for this Chris. I’ve been reading for a long time. Personally I’m not too concerned about the comments section of my site. I just use Disqus and love it. Super simple to manage. Not much customization though which is fine with me. However sometimes my clients want more customization and that’s where this can come in real handy. So thanks again!
I know this is an old post, but I’m still not sure I get why hiding the comment output behind a callback in
wp_list_comments()
is better than the old way, where all of the markup was obvious and included in thecomments.php
file.How can I have a comment box like this on my wordpress site? However, the bold or italic part does not see to work!
Sorry I take it back..it does work..Would love to have this comment box on my website
Thanks for sharing. But I am searching for CSS of the Comment template.
thanks alot
that was useful
Thanks sir. Straight to the point. :)
just noticed that the callback code in your snippet is a bit wrong chris – should be
wp_list_comments("callback"=>"my_custom_comments");
Did that API change? Different best practice?
What’s in use literally on this comment thread right now we’re looking at is: