Skip to content
  • Quick Ref
  • Contact
  • About
wpcanyon.com

wpcanyon.com

Show Top Commentators in WordPress Without a Plugin

Posted on August 20, 2025August 20, 2025 By Admin No Comments on Show Top Commentators in WordPress Without a Plugin

Show Top Commentators in WordPress Without a Plugin

If you want to highlight your most engaged readers by showing the top commentators on your WordPress site, you might think you need a plugin. However, you can achieve this easily with a bit of custom code. This tutorial explains how to show top commentators in WordPress without a plugin, giving you full control and keeping your site lightweight.

Quick Fix: Display Top Commentators with Custom Code

  1. Add a custom function to your theme’s functions.php file or a site-specific plugin to query and display top commentators.
  2. Use a shortcode or call the function directly in your theme templates where you want the list to appear.
  3. Style the output with CSS to match your site’s design.

Why This Happens

WordPress stores all comments in the database, including the author’s name and email. By querying this data, you can count how many comments each user has made and display a ranked list. Plugins automate this, but the underlying process is straightforward and can be done manually with a few lines of code.

Step-by-step: Show Top Commentators Without a Plugin

  1. Backup your site: Before editing theme files, always back up your site or use a child theme.
  2. Open your theme’s functions.php file: Access this via Appearance > Theme Editor or FTP.
  3. Add the following function to fetch and display top commentators:
function get_top_commentators( $number = 5 ) {
    global $wpdb;

    // Query to get commenters and their comment counts
    $results = $wpdb->get_results( "
        SELECT comment_author, comment_author_email, COUNT(comment_author_email) AS comment_count
        FROM $wpdb->comments
        WHERE comment_approved = 1
          AND comment_author_email != ''
        GROUP BY comment_author_email
        ORDER BY comment_count DESC
        LIMIT %d
    ", ARRAY_A, $number );

    if ( empty( $results ) ) {
        return '<p>No commentators found.</p>';
    }

    $output = '<ul class="top-commentators">';
    foreach ( $results as $commentator ) {
        $avatar = get_avatar( $commentator['comment_author_email'], 48 );
        $name = esc_html( $commentator['comment_author'] );
        $count = intval( $commentator['comment_count'] );

        $output .= "<li>{$avatar} <strong>{$name}</strong> ({$count} comment" . ( $count > 1 ? 's' : '' ) . ")</li>";
    }
    $output .= '</ul>';

    return $output;
}

// Shortcode to display top commentators anywhere
function top_commentators_shortcode( $atts ) {
    $atts = shortcode_atts( array(
        'number' => 5,
    ), $atts, 'top_commentators' );

    return get_top_commentators( intval( $atts['number'] ) );
}
add_shortcode( 'top_commentators', 'top_commentators_shortcode' );
  1. Use the shortcode [top_commentators number="5"] in posts, pages, or widgets to display the top 5 commentators.
  2. Or call echo get_top_commentators(5); directly in your theme files where you want the list.

Discussion Settings & Styling

Make sure your WordPress Discussion Settings allow comments and that comments are approved to be counted.

To style the list, add CSS to your theme’s stylesheet or Customizer:

.top-commentators {
    list-style: none;
    padding: 0;
    margin: 0;
}

.top-commentators li {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
}

.top-commentators img.avatar {
    border-radius: 50%;
    margin-right: 10px;
}

This CSS makes the avatars circular and aligns the list items neatly.

Common Pitfalls

  • No comments showing: Ensure comments are approved and not spam.
  • Empty author emails: Comments without an email are ignored to avoid counting anonymous users multiple times.
  • Function placement: Adding code to a parent theme’s functions.php may be lost on updates. Use a child theme or site-specific plugin.
  • Performance: On very large sites, the query might be slow. Consider caching the output.

Test & Verify

  1. Place the shortcode [top_commentators] on a test page.
  2. Visit the page and verify the list shows commenters with avatars and comment counts.
  3. Test with different number values, e.g. [top_commentators number="10"].
  4. Check on mobile and desktop to confirm styling.
  5. Approve new comments and refresh to see the list update.

Wrap-up

Showing top commentators in WordPress without a plugin is simple and efficient. By adding a custom function and shortcode, you can engage your community and showcase your most active readers without bloating your site. Customize the code and styling to fit your theme and enjoy a more interactive site.

Works on

Environment Compatibility
Web Servers Apache, Nginx, LiteSpeed
Hosting Panels cPanel, Plesk, DirectAdmin
WordPress Versions 4.9 and above (tested on latest versions)

FAQ

Q: Can I limit the list to commenters on a specific post or category?
A: The provided code counts all approved comments site-wide. To limit by post or category, the query needs customization using comment_post_ID or joining with post taxonomy tables.
Q: How do I update the list automatically?
A: The list updates dynamically each time the page loads. For better performance on busy sites, consider caching the output with transients.
Q: Can I show commenters’ website links?
A:
Comments Tags:Comments, Engagement, WordPress

Post navigation

Previous Post: How to Style Admin Comments in WordPress
Next Post: Solution previous_posts_link and next_posts_link Not Working

Leave a Reply Cancel reply

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

Recent Posts

  • Top WordPress Themes for Blogs in 2025
  • WordPress Admin Panel Trick: Adding ID Field to the Posts Listing
  • Solution previous_posts_link and next_posts_link Not Working
  • Show Top Commentators in WordPress Without a Plugin
  • How to Style Admin Comments in WordPress

Recent Comments

    Archives

    • August 2025

    Categories

    • Admin & Blocks
    • Admin & UI
    • Automation
    • Automation & Plugins
    • Comments
    • Comparisons
    • Database & Revisions
    • Developer Snippets
    • Fixes & Errors
    • Media & Thumbnails
    • Queries & Pagination
    • Security
    • Speed & Security
    • Tips & Tricks
    • WooCommerce How‑tos
    • WordPress Snippets
    • WordPress Themes
    • Terms & Conditions
    • Affiliate Disclosure

    Copyright © 2025 wpcanyon.com.

    Powered by PressBook WordPress theme