Obfuscate email addresses

Posted by on Oct 14, 2011 in Snippet Friday

Spam is a problem and we know putting email addresses on websites are not the best way to prevent it. This snippet will help keep those spam bots from violating your bubbles. First lets write out the short code in our themes function.php:

function cwc_mail_shortcode( $atts , $content=null ) {
    for ($i = 0; $i < strlen($content); $i++) $encodedmail .= "&#" . ord($content[$i]) . ';';
    return '<a href="mailto:'.$encodedmail.'">'.$encodedmail.'</a>';
}
add_shortcode('mailto', 'cwc_mail_shortcode');

Using the short code is super easy, try it:

[mailto]email@yourdomain.com[/mailto]

Now your email will show up in the source code like this:

&amp;#101;&amp;#109;&amp;#97;&amp;#105;&amp;#108;&amp;#64;&amp;#101;&amp;#109;&amp;#97;&amp;#105;&amp;#108;&amp;#46;&amp;#99;&amp;#111;&amp;#109;

Source: http://blue-anvil.com/archives/8-fun-useful-shortcode-functions-for-wordpress/ & http://www.catswhocode.com/blog/10-super-useful-wordpress-shortcodes