How to Display the Total Number of Spam Comments in WordPress

Home » Snippets » How to Display the Total Number of Spam Comments 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

If you’re a website owner or administrator, you know how important it is to manage spam comments on your site. Spam comments can harm your site’s reputation and also affect its performance. One way to keep track of how well you’re doing in fighting spam is to display the total number of spam comments on your WordPress site. In this article, we’ll show you how to display the total number of spam comments on your site using a few easy steps.

					function wpturbo_display_spam_comment_count() {
    $total_comments = wp_count_comments();
    $total_spam_comments = $total_comments->spam;
    echo "<p>" . sprintf(__( 'Total spam comments: %s', 'wpturbo' ), $total_spam_comments) . "</p>";
}
add_action( 'admin_notices', 'wpturbo_display_spam_comment_count' );
				

In this code snippet, we are going to create a function that will display the total number of spam comments in the WordPress admin dashboard.

First, we create a new function called wpturbo_display_spam_comment_count(). The first thing we do in this function is to get the total number of comments on the WordPress site. We achieve this using the built-in WordPress function wp_count_comments() function. This returns an object that contains the total number of comments, including spam.

$total_comments = wp_count_comments();

Next, we access the spam property of the $total_comments object to get the total number of spam comments on the site.

$total_spam_comments = $total_comments->spam;

At this point, we have the total number of spam comments on our WordPress site. To display this number on the admin dashboard, we use the echo statement with some HTML markup and the sprintf() function.

echo "<p>" . sprintf(__( 'Total spam comments: %s', 'wpturbo' ), $total_spam_comments) . "</p>";

The __() function is another built-in WordPress function that helps make the code translatable. It takes two parameters: the text to be translated and the text domain. In our case, the text to be translated is the string "Total spam comments: %s", which will be displayed on the admin dashboard. The %s is a placeholder that will be replaced by the actual number of spam comments found. The text domain is used for translation purposes.

The sprintf() function allows us to substitute the %s placeholder with the actual number of spam comments. The value of $total_spam_comments is passed to the sprintf() function as the second parameter.

Finally, we add an action hook add_action( 'admin_notices', 'wpturbo_display_spam_comment_count' ). This hook is used to display the custom function wpturbo_display_spam_comment_count on the admin dashboard whenever WordPress needs to display admin notices.

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