Skip to content

Instantly share code, notes, and snippets.

@chuckreynolds
Created April 26, 2017 22:43
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuckreynolds/867d74ea351623bc92b4202039ad9a65 to your computer and use it in GitHub Desktop.
Save chuckreynolds/867d74ea351623bc92b4202039ad9a65 to your computer and use it in GitHub Desktop.
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