If you run your own website, you know the problem: as soon as you publish your email address on it, the first spam messages follow shortly after. They do not just arrive through the contact form, but are often sent directly as well. The reason is that spammers run their own web crawlers that blindly search websites for email addresses so they can bombard them with messages. Since we already dealt with contact form spam some time ago, today is about automatically encrypting email addresses on your own WordPress website.
To hide email addresses from spambots, we use the antispambot() function of WordPress. This function converts the string you pass to it into HTML entities. It turns the classic:
<a href=mailto:deinemail@domain.com>deinemail@domain.com</a>
into the following:
![]()
Although the source code now outputs only HTML entities, you will not notice any change in the frontend itself. Email addresses are still displayed correctly, and mailto links remain clickable as usual. This is done with the code below, which you can add yourself. Alternatively, you will find a download link at the end of the page for a plugin that you can install directly in WordPress.
function osu_encode_all_emails($content) {
// Regular expression to find email addresses
$pattern = '/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/';
// Callback function to encode email addresses
$callback = function ($matches) {
return antispambot($matches[0]);
};
// Apply the regular expression and callback to the content
$content = preg_replace_callback($pattern, $callback, $content);
return $content;
}
// Hook the function into the_content filter
add_filter('the_content', 'osu_encode_all_emails', 99999);
After adding the code, you should test your contact forms and the most important parts of your website. As described, all email addresses are filtered, which can occasionally cause unexpected problems.
As promised, you will find the fully standalone plugin below. You can upload and install the zip file through the WordPress backend as usual.




