How to Display Extra Data in the Recent Comments List Without a Plugin in WordPress

Home » Snippets » How to Display Extra Data in the Recent Comments List Without a Plugin in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Comments are a key feature of WordPress, providing a way for visitors to engage with your content and for you to build a sense of community around your site. By default, WordPress displays a list of recent comments in your sidebar or widget area, showing the commenter’s name, the date of the comment, and a snippet of the comment text. However, what if you want to display additional data alongside the recent comments, such as the post title or category? In this article, we’ll show you how to add extra data to your recent comments list without the need for a plugin.

					function wpturbo_recent_comments_extras( $comment ) {
    $comment_author_url = get_comment_author_url( $comment->comment_ID );
    $comment_link = get_comment_link( $comment->comment_ID );
    echo '<ul><li>Author URL: ' . $comment_author_url . '</li><li>Comment Link: ' . $comment_link . '</li></ul>';
}

function wpturbo_recent_comments_list_extras( $comments ) {
    foreach ( $comments as $comment ) {
        wpturbo_recent_comments_extras( $comment );
    }
}

add_filter( 'wp_list_comments_args', function( $args ) {
    $args['callback'] = 'wpturbo_recent_comments_list_extras';
    return $args;
});
				

In this article, we will show you how to display extra data in the recent comments list on your WordPress site, without the need for a plugin. By default, WordPress displays the comment author name, the date, and the comment content. However, sometimes you may want to have more information on the comment, such as the URL of the author or a link to the comment.

The code snippet we are going to use consists of three functions.

The first function wpturbo_recent_comments_extras( $comment ) is responsible for outputting the extra data we want to display for each individual comment. It accepts as an argument the current comment object.

Within the function, we first use the get_comment_author_url( $comment->comment_ID ) function to get the URL of the comment author. We then use the get_comment_link( $comment->comment_ID ) function to get the link to the comment.

And finally, we output the data in a list format, showing the Author URL and Comment Link.

function wpturbo_recent_comments_extras( $comment ) {
    $comment_author_url = get_comment_author_url( $comment->comment_ID );
    $comment_link = get_comment_link( $comment->comment_ID );
    echo '<ul><li>Author URL: ' . $comment_author_url . '</li><li>Comment Link: ' . $comment_link . '</li></ul>';
}

The second function wpturbo_recent_comments_list_extras( $comments ) loops through all the comments and calls the wpturbo_recent_comments_extras() function for each comment. This function is necessary to ensure that the extra data is displayed for every comment in the list.

function wpturbo_recent_comments_list_extras( $comments ) {
    foreach ( $comments as $comment ) {
        wpturbo_recent_comments_extras( $comment );
    }
}

The third and final part of the code is where we hook the wpturbo_recent_comments_list_extras() function into the wp_list_comments_args filter. This tells WordPress to use the wpturbo_recent_comments_list_extras() function instead of the default callback function when displaying the recent comments list. This is how we are able to add the extra data to the list of comments.

add_filter( 'wp_list_comments_args', function( $args ) {
    $args['callback'] = 'wpturbo_recent_comments_list_extras';
    return $args;
});

And that’s it! With these three functions added to your functions.php file, your WordPress site will now display the additional data in the recent comments list automatically.

Register an account to save your snippets or go Pro to get more features.