Skip to content

Instantly share code, notes, and snippets.

@palpalani
Created June 28, 2014 07:24
Show Gist options
  • Save palpalani/08f73c433c52d4cfe90b to your computer and use it in GitHub Desktop.
Save palpalani/08f73c433c52d4cfe90b to your computer and use it in GitHub Desktop.
WordPress - Cloaking Email Addresses in Your Content
Have you ever needed to share an email address in your WordPress blog or website?
You most likely have, and if you ever cared about not getting caught by spam robots which crawl the web for email addresses, telephone numbers and things like that, you've searched for a solution to hide the email addresses from those tiny evil robots.
I know I have, and little did I know, there was already a core WordPress function for it: antispambot().
Usage
The usage of the function is pretty simple:
<?php
$my_email_address = "my.secret.email.address@gmail.com";
$my_email_address_cloaked = antispambot( $my_email_address );
echo $my_email_address_cloaked;
?>
But of course, you can't use PHP in your content (unless you're using a plugin for that purpose). To use this function in your content, you can utilize a cool little shortcode like the one below:
<?php
function antispambot_sc( $atts ) {
extract( shortcode_atts( array(
'email' => ''
), $atts ) );
return antispambot( $email );
}
add_shortcode( 'antispambot', 'antispambot_sc' );
// Usage: [antispambot email="my.email.address@gmail.com"]
?>
By using the shortcode above, you can cloak email addresses anywhere in your posts/pages/custom post types.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment