How do I remove spam comments with only an email address in WordPress?

Problem:

Delete spam responses with only an email address in WordPress.

Since several days I have been suffering from spam comments below blogs on my website. It often involves 10-20 posts a day, all of which also become visible to my visitors.

The comments consist only of a name + email address and the same email address is also left as a comment.

At first it was at the same blog each time. After 1 day I disabled the comment functionality there. But as I expected, the spam comments were then placed under another blog.

How do I solve this problem?

Solution:

Actually, I expected Akismet to filter this out. After all, I have a paid license for this. I have contacted them but so far the problem has not been resolved.

Blocking the IP address is not an option because they use different IP addresses each time. It are also random email addresses so I can’t filter on that either.

To solve this problem, I wrote the PHP script below:

function child_theme_remove_spam_comments( $idComment, $commentApproved){
    if (1 === $commentApproved) {
        $comment = get_comment($idComment, ARRAY_A);

        if (!str_contains($comment['comment_content'], ' ')) {
            if (str_contains($comment['comment_content'], '@')) {
                $skipTrashbin = false;
                wp_delete_comment($idComment, $skipTrashbin);
            }
        }
    }
}
add_action('comment_post','child_theme_remove_spam_comments', 10, 2);

Clarification:

  1. I added this function to the functions.php file of my child theme
  2. The comment must be approved by WordPress and/or Akismet
  3. The comment must contain only 1 word, which is why I check for a space
  4. If the comment contains 1 word and an @ sign I delete the spam comment

For analysis purposes, I do not permanently delete responses.

I also hope that Akismet will filter out these spam comments in the future so that the above PHP script is no longer needed.

Getest op:

WordPress 6.6.2

Laat een reactie achter

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

Deze site gebruikt Akismet om spam te verminderen. Bekijk hoe je reactie-gegevens worden verwerkt.