Created
April 26, 2017 22:43
-
-
Save chuckreynolds/867d74ea351623bc92b4202039ad9a65 to your computer and use it in GitHub Desktop.
Antispambot WordPress shortcode function
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
<?php | |
/** | |
* Hide email from Spam Bots using a shortcode. | |
* Anti-Spambot Email Shortcode, v1.1.1 | |
* https://wordpress.org/plugins/antispambot | |
* | |
* @param array $atts Shortcode attributes. Not used. | |
* @param string $content The shortcode content. Should be an email address. | |
* | |
* @return string The obfuscated email address. | |
*/ | |
function wpcodex_hide_email_shortcode( $atts , $content = null ) { | |
$email = strtok($content,'?'); //allow e.g. ?subject=foo+bar&body=bif in email | |
$urlpart = strtok(''); | |
if (strlen($urlpart) > 0) { | |
$urlpart = '?'.$urlpart; | |
} | |
if ( ! is_email( $email ) ) { | |
return $content; | |
} | |
extract( shortcode_atts( array( | |
'hex_encoding' => 0, | |
'nolink' => false, | |
'linktext' => false, | |
), $atts, 'antispambot' ) ); | |
if ($nolink) { | |
return antispambot( $email, $hex_encoding ); | |
} else { | |
$url = esc_url('mailto:' . antispambot( $email, $hex_encoding ) . $urlpart ); | |
if ($linktext) { | |
return '<a href="'. $url . '">' . htmlspecialchars($linktext) . '</a>'; | |
} else { | |
return '<a href="'. $url . '">' . antispambot( $email, $hex_encoding ) . '</a>'; | |
} | |
} | |
} | |
add_shortcode( 'email', 'wpcodex_hide_email_shortcode' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment