How to customize the comments template or wp_list_comments()

From wordpress 2.7 version we got the functionality of changing or customizing the comments template as per your theme. As we all know in single.php file we called following function to add comments functionality


<?php comments_template(); ?>

This function basically calls the comments.php file and add the data from wordpress database.

If we check the comments.php file. Usually we are having following content in comments.php file


<?php

// Do not delete these lines
 if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
 die ('Please do not load this page directly. Thanks!');

 if ( post_password_required() ) { ?>
 <p>This post is password protected. Enter the password to view comments.</p>
 <?php
 return;
 }
?>

<!-- You can start editing here. -->
<div id="comment">
<?php if ( have_comments() ) : ?>
 <h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to “<?php the_title(); ?>”</h3>

 <div>
<div><?php paginate_comments_links(); ?></div>
 </div>

 <ol>
 <?php wp_list_comments('callback=wordpressapi_comments'); ?>

 </ol>

 <div>
<div><?php paginate_comments_links(); ?></div>
 </div>
 <?php else : // this is displayed if there are no comments so far ?>

 <?php if ('open' == $post->comment_status) : ?>
 <!-- If comments are open, but there are no comments. -->

 <?php else : // comments are closed ?>
 <!-- If comments are closed. -->
 <p>Comments are closed.</p>

 <?php endif; ?>
<?php endif; ?>

<?php if ('open' == $post->comment_status) : ?>

<div id="respond">

<h3><?php comment_form_title( 'Leave a Reply', 'Leave a Reply to %s' ); ?></h3>

<div>
 <small><?php cancel_comment_reply_link(); ?></small>
</div>

<?php if ( get_option('comment_registration') && !$user_ID ) : ?>
<p>You must be <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=<?php echo urlencode(get_permalink()); ?>">logged in</a> to post a comment.</p>
<?php else : ?>

<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">

<?php if ( $user_ID ) : ?>

<p>Logged in as <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>. <a href="<?php echo wp_logout_url(get_permalink()); ?>" title="Log out of this account">Log out &raquo;</a></p>

<?php else : ?>

<p><input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" size="22" tabindex="1" <?php if ($req) echo "aria-required='true'"; ?> />
<label for="author"><small>Name <?php if ($req) echo "(required)"; ?></small></label></p>

<p><input type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>" size="22" tabindex="2" <?php if ($req) echo "aria-required='true'"; ?> />
<label for="email"><small>Mail (will not be published) <?php if ($req) echo "(required)"; ?></small></label></p>

<p><input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="22" tabindex="3" />
<label for="url"><small>Website</small></label></p>

<?php endif; ?>

<!--<p><small><strong>XHTML:</strong> You can use these tags: <code><?php echo allowed_tags(); ?></code></small></p>-->

<p><textarea name="comment" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p>

<p><input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" />
<?php comment_id_fields(); ?>
</p>
<?php do_action('comment_form', $post->ID); ?>

</form>

<?php endif; // If registration required and not logged in ?>
</div>

<?php endif; // if you delete this the sky will fall on your head ?>
</div>

In the comments loop for showing the comments we used the wp_list_comments() function.

We can customize this function as per our requirment. We can pass the following parameters to this function.


<code> <?php $args = array(
 'walker'            => null,
 'max_depth'         => <em>,
 </em><em>'style' </em><em>=> </em><em>'ul'</em><em>,
 </em><em>'callback' </em><em>=> </em><em>null</em><em>,
 </em><em>'end-callback' </em><em>=> </em><em>null</em><em>,
 </em><em>'type' </em><em>=> </em><em>'all'</em><em>,
 </em><em>'page' </em><em>=> </em>,
 'per_page'          => <em>,
 </em><em>'avatar_size' </em><em>=> </em><em>32</em><em>,
 </em><em>'reverse_top_level' </em><em>=> </em><em>null</em><em>,
 </em><em>'reverse_children' </em><em>=> </em> ); ?> </code>

For customizing the comments follwing parameter is important.

callback (string) The name of a custom function to use to display each comment. Defaults to null. Using this will make your custom function get called to display each comment, bypassing all internal WordPress functionality in this respect. Use to customize comments display for extreme changes to the HTML layout. Not recommended.

In my theme I used that in following way. I changed and passed parameter in comments.php file


<?php wp_list_comments('callback=wordpressapi_comments'); ?>

Then Open the functions.php file put following code in that. This code I am using as per wordpress api.


function wordpressapi_comments($comment, $args, $depth) {
 $GLOBALS['comment'] = $comment; ?>
 <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
 <div id="comment-<?php comment_ID(); ?>">
 <div>
 <?php echo get_avatar($comment,$size='32',$default='<path_to_url>' ); ?>
 <?php $user_name_str = substr(get_comment_author(),0, 20); ?>
 <?php printf(__('<cite><b>%s</b></cite> <span>says:</span>'), $user_name_str) ?>
 </div>
 <?php if ($comment->comment_approved == '0') : ?>
 <em><?php _e('Your comment is awaiting moderation.') ?></em>
 <br />
 <?php endif; ?>

 <div><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>"><?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),'  ','') ?></div>

 <?php comment_text() ?>

 <div>
 <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
 </div>
 </div>
<?php
 }

Many times some people add the full URL of website in the name parameter. That time showing author name of comment writer is looks so ugly. So I used following trick in this function. Using this line of code we can show only 20 characters of author.


<?php $user_name_str = substr(get_comment_author(),0, 20); ?>
 <?php printf(__('<cite><b>%s</b></cite> <span>says:</span>'), $user_name_str) ?>

I changed the this function as per my choose. You can customize this function as per your theme requirement.

Incoming search terms:

You may like following Articles!

83 thoughts on “How to customize the comments template or wp_list_comments()

  1. Get the plugin WP-DB-Backup. Install this into your WordPress blog under the plugin folder and activate it. Under ‘Tools’ there will be a Backup button that you can press. This is the area where you can set up an option to have this database emailed to you on a daily basis or at least once each week. Make sure you use an email that is not connected to your current hosting account in case the whole host gets attacked. Now you have your database automatically backed up.

  2. Hello,Fantastic article dude! i am Fed up with using RSS feeds and do you use twitter?so i can follow you there:D.
    PS:Do you considered putting video to this web site to keep the visitors more entertained?I think it works.Best regards, Oliver Hano

  3. It does seem that everybody is into this kind of stuff lately. Don’t really understand it though, but thanks for trying to explain it. Appreciate you shedding light into this matter. Keep it up

  4. hello there, i just discovered your blog on bing, and i would like to comment that you write exceptionally good on your site. i am really motivated by the method that you compose, and the message is good. in any case, i would also love to acknowledge whether you would love to exchange links with my web portal? i will be more than happy to reciprocate and put your link off in the link exchange area. looking for your response, i give my sincere thanks and enjoy your day!

  5. I was looking for crucial information on this subject. The information was important as I am about to launch my own portal. Thanks for providing a missing link in my business.

  6. I admire what you have done here. I like the part where you allege you are doing this to come apart without hope but I would try on through all the comments that this is working also in behalf of you as well.

  7. gooday there, i just discovered your website via yahoo, and i must say that you write exceptionally well on your web portal. i am truly motivated by the mode that you express yourself, and the subject is great. anyways, i would also love to acknowledge whether you would like to exchange links with my site? i will be certainly more than happy to reciprocate and put your link off in the link exchange area. looking for your answer, i would like to convey my appreciation and cheers!

  8. A useful insight into How to customize the comments template or wp_list_comments() | WordPressapi.com – Developer Code book and ideas I will use in my blog. You’ve obviously taken some time on this. Many thanks!

  9. You ought to seriously think about working on growing this blog into a dominant voice in this field. You obviously have a solid handle of the topics all of us are searching for on this site anyways and you could possibly even make a buck or two off of some offers. I would dive into following recent trends and raising the volume of blog posts you put up and I bet you’d begin getting some awesome traffic soon. Just a brainstorm, good luck regardless!

  10. I can guess the hard work it must have been required to research for this post.All what i can say is just keep providing such post we all love it.And just to bring something to your notice,I have seen several blog providng your blog as source for this information.

  11. Your report has extra excellent value in your website. I say this due to the fact to me personally I find it priceless. Maybe to some 1 else it is not but to me you did very good. Thanks for your data.

  12. A truly very good publish by you my pal. I’ve bookmarked this web page and can are available again following several days to examine for just about any new posts that you simply make.

  13. I would like to say, nice webpage. Im not sure if it has been addressed, however when using Firefox I can never get the entire page to load without refreshing alot of times. Could just be my modem.

  14. Brilliant blog posting. I found your post very interesting, I think you are a brilliant writer. I added your blog to my bookmarks and will return in the future.

  15. Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog. I am sure my visitors will find that very useful.

  16. Wow!, this was a real quality post. In theory I’d like to write like this too – taking time and real effort to make a good article… but what can I say… I keep putting it off and never seem to achieve anything

  17. Because of the obnoxious “Tweet/Facebook share/Buzz/Digg/etc” bar that hovered next to everything while I visited this page. I couldn’t actually concentrate on the information that brought me here. I even had to scroll tactfully to be able to see what I was writing in this comment field.

    Because of that, I will never visit this website. It annoyed me enough, that I felt compelled to even write a comment saying so. I’m not sure what was even on this page, just an annoying bar telling me to promote this site on social sites.

    Nice work, push away readers by trying to promote your site to get more readers. Not a very good UI plan.

  18. You, Sony, are the most important blogger at this moment for me =D

    I’ve been searching around 2 hours around the net this and with no luck. I think there isn’t another easy way to get your comments like you want.

    Thank you so much.

  19. I really like my topic, My organization is with internet marketing/SEO. Have you ever heard from meetup.com? I find that there is usually a few decent probabilities to fulfill and netwoork online websites to our destinations. You ought to check it out should you receive period.

  20. I am confused with the code comment in comment.php WPtypo.
    Is it possible to make a comment thread on the theme that the code is
    not wp_list_comments WPtypo (). ?

    List Code Comment,php WPtypo

    <li class="clearfixuser_id == $post->post_author)
    { echo ‘ author’; } ?>” id=”comment-”>
    user_id === $post->post_author) echo ”; ?>

    && ‘Reply’,
    ‘add_below’ => $add_below, ‘depth’ => $depth, ‘max_depth’ =>
    $args['max_depth']))); ?> user_id ===
    $post->post_author) { echo “Penulis“; } ?>

    comment_approved == ’0′) : echo
    Komentarnya saya simpen dulu di lemari yah
    !!!“; endif; ?>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>