Jan
11
2012

Shortcode to Encode Email in WordPress Posts

by   |  Posted in Tutorials  |  1 comment

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

About the author:

A freelance web developer living in Montreal who spends most of his time writing for this site and building Premium themes for WordPress. You can find him on Twitter @bavotasan.

Site5 Affiliate Link
If you liked this, please share it.

Tags: , , ,

Short URL: http://bit.ly/xAr0g7

Discussion 1 Comment

  1. Design Inspiration on January 13, 2012 at 9:24 am

    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.

Leave a Reply

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

*


To enter code in the comment box, please place it between <pre lang="php"> </pre> tags. You don't have to convert any characters to their hex/HTML code. Just add your code the way you would to any code editor.