Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Antispambot WordPress shortcode function
<?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