It is easy to stop most WordPress comment spam instantly with one small change to your theme’s functions.php. This is one of my favourite WordPress functions and I use it on all my setups.

This code snippet works by blocking comments from being added offsite or indirectly and not from the comment box on your webpage.

Just to prove this works - take a look at your spam comments now and mentally calculate your daily average of new spam comments. Then after adding this script wait for 24 hours - if your spam comments are not way down or close to zero you’ve done something wrong.

The role of WordPress functions.php

Your functions.php file is the place to add snippets of code just like this. Remember to keep a copy incase you have to update or replace your theme.

Stop WordPress Comment Spam

Go to your theme folder and open functions.php and insert this code


function</span> verify_comment_referer<span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span><span style="color: #339933;">!</span>wp_get_referer<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
        wp_die<span style="color: #009900;">(</span> __<span style="color: #009900;">(</span><span style="color: #0000ff;">'You cannot post comment at this time,
may be you need to enable referrers in your browser.'</span><span style="color: #009900;">)</span> <span style="color: #009900;">)</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">}</span>
<span style="color: #009900;">}</span>
add_action<span style="color: #009900;">(</span><span style="color: #0000ff;">'check_comment_flood'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'verify_comment_referer'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>

Do you like this? Did this work for you?

Check back later for more helpful suggestions.

See Also: Clickon5.org

Tags: Comments, Functions, Spam

Read more from my blog for an introduction and quick tips on developing in Hugo or UCTD.

Meet the author

Photo for Damien Saunders
Damien Saunders
An experienced management consultant and business leader interested in digital transformation, product centred design and scaled agile. If I'm not writing about living with UCTD (an autoimmune disease), I'm probably listening to music, reading a book or learning more about wine.