How to make your emails in WordPress content more secure

Securing emails is crucial for every site. WordPress has a beautiful solution for securing your email links. This core function is called antispambot(). With a simple shortcode, which you can add to a custom plugin or your theme function.php you can use this function.

After processing a email address by the antispambot function:

echo antispambot('john.doe@mysite.com');

The output in HTML is like this:

john.doe@mysite.com

But normal visitors see this:

john.doe@mysite.com

Use Antispambot in a shortcode

We use the following code in our functions.php to make secure emails possible for all our clients. If you want to use it, please feel free to copy paste the following code to your own functions.php or in a custom plugin.


<?php
/* ------------------------------------------------------------------*/
/* SECURE EMAIL SHORTCODE */
/* ------------------------------------------------------------------*/

function wpf_secure_mail( $atts,$content=null ) {
extract(
shortcode_atts( array(
"mailto" => ''
), $atts)
);

if(!$content) $content=$mailto;

$mailto = antispambot( $mailto );
$content = antispambot( $content );

return '<a href="mailto:' . $mailto . '">' . $content . '</a>';
}

if ( function_exists('add_shortcode') )
add_shortcode('email', 'wpf_secure_mail');

?>

To use this shortcode use this in your WordPress content:

[email mailto="john.doe@mysite.com"]
or
[email mailto="john.doe@mysite.com"]Click here[/email]

Just post your comment if you’ve any questions about this WordPress tip or if you see a mistake.