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("youremail@yoursite.com");

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 ''.antispambot($content).'';
}
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

Share this:

Email
Facebook
Twitter
Pinterest
Pocket

Premium Themes for WordPress

Looking for an easy-to-use Premium Theme for WordPress? Check out Themes by bavotasan.com and have your site up and running in no time.

Use this WordPress website builder to build powerful websites in no time for your or your clients.

WordPress Hosting

WP Engine – designed from the ground-up to support and enhance the workflow of web designers.

Bluehost – providing quality web hosting solutions since 1996.

About the author

Picture of Luke Perrie

Luke Perrie