Shortcode to Encode Email in WordPress Posts
A while back I wrote a short tutorial entitled “A Clever and Safe Way to Display your Email Address” which explained how you could use CSS to safely display your email on a web page. That way still works well but if you’re using WordPress, you can take advantage of a function called antispambot() to display emails addresses anywhere on your site.
If you code a page template directly, you can add the function like so:
antispambot("[email protected]"); |
I needed a away to take advantage of the antispambot() function within my post and page content, so I decided to create a shortcode that would make it super easy to reuse.
// EMAIL ENCODE SHORTCODE function email_encode_function( $atts, $content ){ return '<a href="'.antispambot("mailto:".$content).'">'.antispambot($content).'</a>'; } add_shortcode( 'email', 'email_encode_function' ); |
Once you add the above code snippet to your theme’s function.php file you can use the following structure to safely display an email address in your post or page content:
[email]you@you.com[/email] |
You can read more about antispambot() in the codex: http://codex.wordpress.org/Function_Reference/antispambot




First things first, I am totally in love with your design.
Secondly, I’ve twittered your post. That explains how useful I feel it is. Thanks for sharing.
Hey
I dont’t use WP
I have own CMS and I see that i must write simillar plugin
like this
There will be much less spam
thx for tip
Great find ! I wish I read this BEFORE my email address got on every mailing list of the interwebz
.
This looks promising. I’m just wondering how would I implement the code if I want to have, for example, Please click (here) to email — with the encoded email address in bracketed word? Thanks!
You would have to rewrite the function so that the return line ends up returning the mail to link with the text “here” instead of the actual email address.
This is a cool feature in WordPress, I didn´t know about that. I protect my email-adress by including it as an image, the antispambot function looks more professional.