Encode Email Address to prevent Spam in WordPress

Help others by sharing this article

When we think about Encode Email Address, you may have noticed that when you look at the markup of your WordPress site, there’s an email address in plain text. That’s not great if you don’t want people to be able to harvest the email addresses of your users. Encoding your email addresses is a good first step for hiding them from spammers.

This post will show you how to hide email addresses from the markup in WordPress without a plugin. You’ll need to be comfortable editing your theme functions.php file, but it shouldn’t take more than five minutes.

But before starting you should know, what is Email Encoding? and Why Encode Email Addresses?

Table of Contents

What is Email Encoding?

Email encoding is the process of taking a piece of information and making it unreadable to web crawlers.

Email addresses are frequently encoded because they are so often used for spamming. So, if a website has an email address listed on the site, then the website will be at risk of being penalized by search engines, or even blacklisted, for having spammy content. Luckily, there’s a way to prevent this from happening to your own website.

Why Encode Email Addresses?

It’s important to encode email addresses because it protects them from bots that might be crawling through your site and collecting email addresses for spam. If you don’t encode the address, the bot will pick it up and add it to their list. This means that your readers will start getting a lot of spam, and they probably won’t like it very much. And if they don’t like it, they probably won’t come back to your site again.

Also Check: Remove WordPress Version Number Without Plugin

wpamit hide or encode email address in wordpress without plugin by adding snippet in functions.php file 2
Result of the snippet shared in this article.

Steps to Encode Email Address in WordPress

Before starting, you should:

  • Take a full backup of your website.
  • If you are not comfortable editing functions.php file then install this free plugin and add the snippet by using a single plugin.
  • Use a child theme to make customizations. Use this free plugin to create a child theme (you can delete the plugin after creating child theme, but do not delete the parent theme).

Step 1: Go to WordPress Dashboard > Appreance > Theme File Editor. Then Click on Theme Functions or functions.php file.

Below is the image for your reference.

how to access theme file editor
Access Theme File Editor
how to access theme functions or functions.php
Access functions.php

Step 2: Add a piece of code mentioned below to the footer of functions.php file.

//Encode Email address to prevent spam
function wpamit_encode_email_shortcode($atts, $content = null) {
	for ($i = 0; $i < strlen($content); $i++) $encodedmail .= "&#" . ord($content[$i]) . ';';
	return '<a class="encoded-email" href="mailto:' . $encodedmail . '">' . $encodedmail . '</a>';
add_shortcode('email', 'wpamit_encode_email_shortcode');

By using this code, we have created a shortcode ’email’ to share the email addresses in posts or pages.

To display an encoded email address, use the shortcode like this:

[email][email protected][/email]

obviously, you need to change the email address with yours.

For this tutorial, I added my email address to my contact page by using the shortcode. Below is the image for your reference.

wpamit hide or encode email address in wordpress without plugin by adding snippet in functions.php file 3

This is how we can encode email address in WordPress without plugin. If you have any queries, please comment below.

Help others by sharing this article

Leave a Reply

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