So, lets assume you want to paste your email all over your website, but you dont want those nasty crawlers getting a hold of it (you get enough spam already right?)
A solution I have used for many of my sites over the years is to obfusticate the email address so that web crawlers cannot read it directly from the source.
The below HTML represents what the output looks like, and as you can see, no visible email address can be found.
|
1
|
<a href="#" class="encrypted" rel="104102110111115069111117126110124127113113113119113128133065119132131">Email Me</a> |
Drop the code function in your themes functions.php:
|
1
2
3
4
5
6
7
|
/* Email Encryption */function email_encrypt( $atts, $caption ) { extract( shortcode_atts( array( 'address' => 'invalid@email.com' ), $atts ) ); for($i=0,$o='';$i<strlen($address);$i++) $o.= str_pad( ord( substr( $address, $i, 1 ) ) + $i, 3, 0, STR_PAD_LEFT ); return sprintf( '<a href="#" rel="%s">%s</a>', $o, $caption );}add_shortcode( 'email', 'email_encrypt' ); |
Then you will also need to place this Javascript somewhere, if you have a Javascript file associated to your theme, use that, if not, add this code to the footer.php:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<script type="text/javascript">jQuery(document).ready(function(){ jQuery(".encrypted").each(function(i,el){ var string = jQuery(el).attr('rel'); for(var i=0, nstring = '';i<string.length;i+=3) nstring += String( String.fromCharCode(string.substring(i,i+3)-(i/3)) ); jQuery(el).removeClass("encrypted").addClass("plaintext").attr("href","mailto: " + nstring); if ( jQuery(el).text() == "original" ) jQuery(el).text(nstring); });});</script> |
IF you did add it to a previously existing Javascript file, you can omit the <script> tags.
You can now protect emails in your posts using the following format:
|
1
|
[email address="test@test.com"]Email me![/email] |
A little note, if you would like the outputted HTML text to be the email address, enter “original” as the text.
I hope this helps you from those naughty spam bots. You can see this snippet in use at my buddies site — Michal Kopanski, he made me develop it.
Source: tutorialist.net