Shortcode to Encode Email in WordPress Posts

Posted In Web Development - By On Wednesday, January 11th, 2012 With 0 Comments

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 '<a href="'.antispambot("mailto:".$content).'">'.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

Shortcode to Encode Email in WordPress Posts

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>