Sanitarium by Zac Dreyer

Mod: Shortcode to Encode Email in WordPress Posts and Pages

Posted by zac on Saturday, October 20th, 2012

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

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 reuseable.
 
Code (add to function.php):

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

Code:

[ email ]you@you.com[ /email ]

Posted in: Featured, Wordpress Shizzle.

Comments are closed.