How To Hide MailTo Links From Spambots in WordPress

There may be times where you need to publically display your email address in parts of your website. If you show your email address on a page in your website you run the risk of a spambot crawling your website and adding your email address to a spam list.

The result will be either a daily or weekly barrage of spam emails delivered to your email inbox. This block of code will protect email addresses shown in pages, posts and widgets. Simply add the code below to your WordPress functions.php file in your child theme.

function remove_plaintext_email($emailAddress) {
$emailRegEx = '/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4})/i';
return preg_replace_callback($emailRegEx, "encodeEmail", $emailAddress);

function encodeEmail($result) {
return antispambot($result[1]);
add_filter( 'the_content', 'remove_plaintext_email', 20 );
add_filter( 'widget_text', 'remove_plaintext_email', 20 );

What are WordPress Filters?

Filters are functions that WordPress passes data through. They are primarily responsible for intercepting, managing, and returning data before rendering it to the browser or saving data from the browser to the database. More information on the add_filter WordPress function can be found on the codex:

In our case we want to search the content of our pages/posts for email addresses and apply the WordPress antispambot function before the content is shown in the browser.

What does the WordPress antispambot function do?

The WordPress antispambot function takes an email address and converts the characters to encoded HTML characters.

Web browsers will read the encoded HTML and show the email address to visitors but spambots would be unable to see the email address on the page.

How can You protect email addresses inside Your theme?

If you need to add an email address to one of your theme’s files and you want to protect it from spambots then you need to call the encodeEmail function we created earlier.

  echo encodeEmail(''); 
Affiliate Disclosure:

We may link to products and online services provided by third-parties. Soe of the links that we post on our site are affiliate links, which means that we receive commission if you purchase the item. We will never recommend a product or service that we have not used ourselves. Our reviews will be honest and we will only recommend something if we have found it useful.


Lacey Tech Solutions publish blog articles to help small businesses. We are not liable for any damages if you choose to follow the advice from our blog.

Leave a Comment

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