walker class – bbird.me http://bbird.me WordPress quick tips Wed, 08 Aug 2018 16:39:07 +0000 en-US hourly 1 https://wordpress.org/?v=4.9.8 How to create custom comment walker class in WordPress http://bbird.me/how-to-create-custom-comment-walker-class-in-wordpress/ http://bbird.me/how-to-create-custom-comment-walker-class-in-wordpress/#comments Fri, 03 Jun 2016 20:29:46 +0000 http://bbird.me/?p=513 There are several walker classes in WordPress – I don’t actually know how many there are because I tend to

Post How to create custom comment walker class in WordPress je prvi puta viđen na bbird.me.

]]>
There are several walker classes in WordPress – I don’t actually know how many there are because I tend to browse the source code only if I need to change something, just as others I guess :). Regardless, I’m sure that custom navigation walker class is probably the most used and customized WordPress walker class. But recently I had a project where I needed to change meta info which appears next to Gravatar image in the comments section.  When it comes to any walker classes in WordPress, I guess that the main idea is that you take the whole code, put it into functions.php or elsewhere and use function callback to call it. I guess…

So first things first – the function which calls comments is wp_list_comments() which also accepts number of parameters. One of them is ‘walker’. In order to call walker, it would be nice to create one. You can call it like this:

wp_list_comments( array(
  'style'       => 'ol',
  'short_ping'  => true,
  'avatar_size' => 82,
  'walker' => new bruno_walker_comment
) );

But as I said, we need a walker:

<?php
class bruno_walker_comment extends Walker_Comment {
public $tree_type = 'comment';
public $db_fields = array ('parent' => 'comment_parent', 'id' => 'comment_ID');
public function start_lvl( &$output, $depth = 0, $args = array() ) {
$GLOBALS['comment_depth'] = $depth + 1;
switch ( $args['style'] ) {
case 'div':
break;
case 'ol':
$output .= '<ol class="children">' . "\n";
break;
case 'ul':
default:
$output .= '<ul class="children">' . "\n";
break;
}
}
public function end_lvl( &$output, $depth = 0, $args = array() ) {
$GLOBALS['comment_depth'] = $depth + 1;
switch ( $args['style'] ) {
case 'div':
break;
case 'ol':
$output .= "</ol><!-- .children -->\n";
break;
case 'ul':
default:
$output .= "</ul><!-- .children -->\n";
break;
}
}
public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
if ( !$element )
return;
$id_field = $this->db_fields['id'];
$id = $element->$id_field;
parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
/*
* If at the max depth, and the current element still has children, loop over those
* and display them at this level. This is to prevent them being orphaned to the end
* of the list.
*/
if ( $max_depth <= $depth + 1 && isset( $children_elements[$id]) ) {
foreach ( $children_elements[ $id ] as $child )
$this->display_element( $child, $children_elements, $max_depth, $depth, $args, $output );
unset( $children_elements[ $id ] );
}
}
public function start_el( &$output, $comment, $depth = 0, $args = array(), $id = 0 ) {
$depth++;
$GLOBALS['comment_depth'] = $depth;
$GLOBALS['comment'] = $comment;
if ( !empty( $args['callback'] ) ) {
ob_start();
call_user_func( $args['callback'], $comment, $args, $depth );
$output .= ob_get_clean();
return;
}
if ( ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) && $args['short_ping'] ) {
ob_start();
$this->ping( $comment, $depth, $args );
$output .= ob_get_clean();
} elseif ( 'html5' === $args['format'] ) {
ob_start();
$this->html5_comment( $comment, $depth, $args );
$output .= ob_get_clean();
} else {
ob_start();
$this->comment( $comment, $depth, $args );
$output .= ob_get_clean();
}
}
public function end_el( &$output, $comment, $depth = 0, $args = array() ) {
if ( !empty( $args['end-callback'] ) ) {
ob_start();
call_user_func( $args['end-callback'], $comment, $args, $depth );
$output .= ob_get_clean();
return;
}
if ( 'div' == $args['style'] )
$output .= "</div><!-- #comment-## -->\n";
else
$output .= "</li><!-- #comment-## -->\n";
}
protected function ping( $comment, $depth, $args ) {
$tag = ( 'div' == $args['style'] ) ? 'div' : 'li';
?>
<<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( '', $comment ); ?>>
<div class="comment-body">
<?php _e( 'Pingback:' ); ?> <?php comment_author_link( $comment ); ?> <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?>
</div>
<?php
}
protected function comment( $comment, $depth, $args ) {
if ( 'div' == $args['style'] ) {
$tag = 'div';
$add_below = 'comment';
} else {
$tag = 'li';
$add_below = 'div-comment';
}
?>
<<?php echo $tag; ?> <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?> id="comment-<?php comment_ID(); ?>">
<?php if ( 'div' != $args['style'] ) : ?>
<div id="div-comment-<?php comment_ID(); ?>" class="comment-body">
<?php endif; ?>
<div class="comment-author vcard">
<?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
<?php printf( __( '<cite class="fn">%s</cite> <span class="says">says:</span>' ), get_comment_author_link( $comment ) ); ?>
</div>
<?php if ( '0' == $comment->comment_approved ) : ?>
<em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ) ?></em>
<br />
<?php endif; ?>
<div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
<?php
/* translators: 1: comment date, 2: comment time */
printf( __( '%1$s at %2$s' ), get_comment_date( '', $comment ), get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)' ), '&nbsp;&nbsp;', '' );
?>
</div>
<?php comment_text( $comment, array_merge( $args, array( 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
<?php
comment_reply_link( array_merge( $args, array(
'add_below' => $add_below,
'depth' => $depth,
'max_depth' => $args['max_depth'],
'before' => '<div class="reply">',
'after' => '</div>'
) ) );
?>
<?php if ( 'div' != $args['style'] ) : ?>
</div>
<?php endif; ?>
<?php
}
protected function html5_comment( $comment, $depth, $args ) {
$tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
?>
<<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?>>
<article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
<footer class="comment-meta">
<div class="comment-author vcard">
<?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
<?php printf( __( '%s <span class="says">says:</span>' ), sprintf( '<span class="fn">%s</span>', get_comment_author_link( $comment ) ) ); ?>
</div><!-- .comment-author -->
<div class="comment-metadata">
<?php
$user_id=$comment->user_id;
?>
<p class="commenter-bio"><?php the_author_meta('description',$user_id); ?></p>
<p class="commenter-url"><a href="<?php the_author_meta('user_url',$user_id); ?>" target="_blank"><?php the_author_meta('user_url',$user_id); ?></a></p>
</div><!-- .comment-metadata -->
<?php if ( '0' == $comment->comment_approved ) : ?>
<p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></p>
<?php endif; ?>
</footer><!-- .comment-meta -->
<div class="comment-content">
<?php comment_text(); ?>
</div><!-- .comment-content -->
<?php
comment_reply_link( array_merge( $args, array(
'add_below' => 'div-comment',
'depth' => $depth,
'max_depth' => $args['max_depth'],
'before' => '<div class="reply">',
'after' => '</div>'
) ) );
?>
</article><!-- .comment-body -->
<?php
}}

That’s a lot of code, right? But don’t worry,  About 99% of that code is copy-paste from class-walker-comment.php file, which lives in wp-includes folder. The only change I did was this:

<div class="comment-metadata">

<?php 
$user_id=$comment->user_id;
?>
<p class="commenter-bio"><?php the_author_meta('description',$user_id); ?></p>
<p class="commenter-url"><a href="<?php the_author_meta('user_url',$user_id); ?>" target="_blank"><?php the_author_meta('user_url',$user_id); ?></a></p>
						
</div><!-- .comment-metadata -->

I used this code to get some data ( biographical info and user URL) from Users section. So to conclude, if you want to make some bigger changes in the comments section, walker class is the way to go. All you need to do is copy the original walker and extend it with your own changes.

Post How to create custom comment walker class in WordPress je prvi puta viđen na bbird.me.

]]>
http://bbird.me/how-to-create-custom-comment-walker-class-in-wordpress/feed/ 1