My WordPress website is being bombarded by spam comments. Nearly all of them contain a valid URL.

How can I delete these comments automatically? I don't want to put them into the review queue; I just want to get rid of them completely. I don't care if a few legitimate comments get deleted in the process.

  • I hope you can find another way than deleting legit comments that fill url field. What about trying an honey-trap instead, i.e. introduce a hidden field that might look like an url field to (dummy) bots and don't save the comments that have it filled? Other ways exists too.
    – birgire
    Commented May 22, 2019 at 10:36

2 Answers 2


Well the best way is to use a antispam plugin like Aksimet. Other captcha plugins can be helpful too, you just have to look for the captcha keyword in wordpress repository.

If you want to do this manually, I can give you a hint. First of all, you need to know which action hook you want. I couldn't find an action that fires before the comment insertion, but comment_post runs after that.

Then you need to detect if your comment body contains a url, this SO question can be helpful with that. You can delete a comment with wp_delete_comment function.

  • 1
    another hint, don't let users add comments unless they're logged in. and use comment moderation combined with Akismet
    – Vishwa
    Commented May 22, 2019 at 11:31

You might need to add a plugin (or add your own comment filter) to 'count' the number of URLs in a comment before it is stored.

The Akismet plugin is a good one for catching a lot of spam. For one the sites that I managed, I had a similar problem. I ended up writing a plugin to allow for counting URLs (and lots of other customization of the comment form). It's called "FormSpammerTrap for Comments". It is fairly effective against someone that fills in the comment form.

It is not so effective for someone that uses CURL to directly post to the wp-comments.php page, bypassing the comment form. I haven't found a good way to block that yet; there aren't filters that happen in the proper place to block CURL commenters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.