Jan
11
2012

Shortcode to Encode Email in WordPress Posts

by   |  Posted in Tutorials  |  6 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("[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
Share the love...

Tags: , , ,

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

Discussion 6 Comments

  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.

  2. 3rdeye on March 3, 2012 at 9:21 am

    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 :)

  3. Table de multiplication on March 11, 2012 at 4:51 am

    Great find ! I wish I read this BEFORE my email address got on every mailing list of the interwebz :) .

  4. Mononoke on March 19, 2012 at 12:40 pm

    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!

    • c.bavota on March 20, 2012 at 9:18 am

      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.

  5. Dampfbügelstationen on March 23, 2012 at 12:37 pm

    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.

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.