Created
June 28, 2014 07:24
-
-
Save palpalani/08f73c433c52d4cfe90b to your computer and use it in GitHub Desktop.
WordPress - Cloaking Email Addresses in Your Content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "[email protected]"; | |
$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="[email protected]"] | |
?> | |
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