Obfuscating email addresses in WordPress

Sad though it may be, it can be dangerous to publish your email address online because your privacy is then destroyed (and you’ll be receiving a lot of junk mail moving forward).

The typical way of getting around this is by providing a contact form, where the email address is hidden from the end user. However, there are times when it would be useful to display the email address without the risk of spiders and email harvesting software from picking it up.

WordPress has a built-in function for doing just that, called antispambot().. You can pass any email address to this function and it returns the same email address, but encoded in HTML entities. This makes it harder for harvesting software to recognise it as an email address, but still displays completely normally to the end user.

Since it’s a WordPress function, you can either build it into your theme files, or you can create a shortcode to wrap email addresses to use it within posts, pages and widgets. To do that, drop the following snippet into your functionality plugin:

By way of example, using me@mysite.com inside that shortcode looks normal on the front end, but when you look at the page source (which Google and crawlers etc. read), you’ll see that it’s far from recognisable as an email address:

This is a great way to make your email address public, without making it public, if you know what I mean.

One thought on “Obfuscating email addresses in WordPress”

  1. aldolat says:

    Hi Dave,
    some years ago I wrote a shortcode function for my “functionality” plugin and it’s available here: https://gist.github.com/aldolat/6e234d0baf479f5c6775

    It contains some extra options that could be useful to someone. Also, it lets us define a custom text linked to an email address.


Leave a Reply