How to safely show your email address in WordPress

If you’re new to WordPress or writing online articles in general, you might think leaving your email address in full form is simple and effective since users can contact you by clicking on the address link. Maybe you think you’re safe enough if you don’t link your address and simply display it as it is. But if you do that, you inbox and spam folder will be flooded by spam in a matter of days because spam bots will quickly pick up on your insecure email address.

Before you start worrying about the safety of your inbox, we will show you a quick and easy way of encoding your email address so that you can safely show it anywhere on your site.

Instead of searching for third party plugins or complex solutions, you should know that WordPress developers found a way of encoding your email address and thus making it safe to use. The function designated for the job is called antispambot and it’s very easy to use.

If you’re about to encode the email address somewhere in your template files (like footer.php file for example where you want to show your contact email), here’s what to do:

  1. Open footer.php or any template file you want
  2. Copy and paste the function:
  3. antispambot("youremail@yoursite.com");
  4. Save changes

More likely than not, you will need to display your email address elsewhere. Instead of dealing with PHP code all the time, let’s create a simple shortcode which can be used anywhere in WordPress. In the end, you can even use it in template files.

  1. Open functions.php file
  2. Copy and paste the following:
  3. function safe_email( $atts, $content ) {
    return '<a href="mailto:' . antispambot( $content ) . '">' . antispambot( $content ) . '</a>';
    }
    add_shortcode( 'email', 'safe_email' );
  4. Save changes

The function mentioned above creates a [email] shortcode which encodes the email address written in between the shortcode. Now, every time you want to display your email address in posts, pages, or anywhere on the site, use:

[email] youremail@yoursite.com [/email]

Your email address will stay safe, spam-bots won’t be able to find it that easily and you can relax and enjoy your WordPress website as well as your spam-free inbox.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

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