Site icon Hip-Hop Website Design and Development

Comment Walker vs. Comment Callback

I’ve been messing around with custom comments this week and I’m trying to figure out when I’d want to use a walker and when I’d use a callback…

There’s some things I can’t quite figure out how to change, but the one that’s been driving me mad is how to add classes to the children <ul> tag:

<ul class="children">

Are there certain things a comment walker can do that a comment callback can’t?… or vice-versa? And is one of these “better” to use as a theme developer? For example, I know that a lot of menu plugins don’t work when a theme is using a custom menu walker.

Anyway, here’s an example of the exact same comments, but one is being output using a walker and one is using a callback… What am I missing here? Why the need for both of these?

Comment Callback

wp_list_comments( array(
    'callback'      => 'bootstrap_comment_callback',
 ));

function bootstrap_comment_callback( $comment, $args, $depth ){
        $GLOBALS['comment'] = $comment; ?>
    <li id="comment-<?php comment_ID(); ?>" <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>>

    <?php if ( 0 != $args['avatar_size'] ): ?>
    <div class="media-left">
        <a href="<?php echo get_comment_author_url(); ?>" class="media-object"><?php echo get_avatar( $comment, $args['avatar_size'] ); ?></a>
    </div>
    <?php endif; ?>

    <div class="media-body">
    <?php printf( '<h4 class="media-heading">%s</h4>', get_comment_author_link() ); ?>
    <div class="comment-metadata">
        <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID, $args ) ); ?>">
            <time datetime="<?php comment_time( 'c' ); ?>">
                <?php printf( _x( '%1$s at %2$s', '1: date, 2: time' ), get_comment_date(), get_comment_time() ); ?>
            </time>
        </a>
    </div><!-- .comment-metadata -->

    <?php if ( '0' == $comment->comment_approved ) : ?>
    <p class="comment-awaiting-moderation label label-info"><?php _e( 'Your comment is awaiting moderation.' ); ?></p>
    <?php endif; ?>             

    <div class="comment-content">
         <?php comment_text(); ?>
    </div>

    <ul class="list-inline">
        <?php edit_comment_link( __( 'Edit' ), '<li class="edit-link">', '</li>' ); ?>

    <?php
        comment_reply_link( array_merge( $args, array(
            'add_below' => 'div-comment',
            'depth'     => $depth,
            'max_depth' => $args['max_depth'],
            'before'    => '<li class="reply-link">',
            'after'     => '</li>'
        ) ) );  
    ?>
    </ul>
    </div>  
<?php
}

Comment Walker

wp_list_comments( array(
    'walker'        => new Bootstrap_Comment_Walker(),
 ));

class Bootstrap_Comment_Walker extends Walker_Comment {
    protected function html5_comment( $comment, $depth, $args ) {

?><li id="comment-<?php comment_ID(); ?>" <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>>

    <?php if ( 0 != $args['avatar_size'] ): ?>
    <div class="media-left">
        <a href="<?php echo get_comment_author_url(); ?>" class="media-object"><?php echo get_avatar( $comment, $args['avatar_size'] ); ?></a>
    </div>
    <?php endif; ?>

    <div class="media-body">
    <?php printf( '<h4 class="media-heading">%s</h4>', get_comment_author_link() ); ?>
    <div class="comment-metadata">
        <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID, $args ) ); ?>">
            <time datetime="<?php comment_time( 'c' ); ?>">
                <?php printf( _x( '%1$s at %2$s', '1: date, 2: time' ), get_comment_date(), get_comment_time() ); ?>
            </time>
        </a>
    </div><!-- .comment-metadata -->

    <?php if ( '0' == $comment->comment_approved ) : ?>
    <p class="comment-awaiting-moderation label label-info"><?php _e( 'Your comment is awaiting moderation.' ); ?></p>
    <?php endif; ?>             

    <div class="comment-content">
         <?php comment_text(); ?>
    </div>

    <ul class="list-inline">
        <?php edit_comment_link( __( 'Edit' ), '<li class="edit-link">', '</li>' ); ?>

    <?php
        comment_reply_link( array_merge( $args, array(
            'add_below' => 'div-comment',
            'depth'     => $depth,
            'max_depth' => $args['max_depth'],
            'before'    => '<li class="reply-link">',
            'after'     => '</li>'
        ) ) );  
    ?>
    </ul>
    </div>      
<?php
    }   
}