Skip to content

Instantly share code, notes, and snippets.

@intelliweb
Last active December 16, 2015 00:39
Show Gist options
  • Save intelliweb/5349548 to your computer and use it in GitHub Desktop.
Save intelliweb/5349548 to your computer and use it in GitHub Desktop.
WP: encodes email address by converting each character into HTML entities to help protect against spambots
<?php
// WordPress function that encodes an email address (and adds mailto: link)
echo antispambot('protectmy@email.com', 1);
?>
<?php
// Shortcode to encode email address in a mailto: link
function protect_email_address( $atts , $content=null ) {
for ($i = 0; $i < strlen($content); $i++) $encodedmail .= "&#" . ord($content[$i]) . ';';
return '<a href="mailto:'.$encodedmail.'">'.$encodedmail.'</a>';
}
add_shortcode('mailto', 'protect_email_address');
/*
Then use this shortcode format to 'encode' it:
[mailto]protectmy@email.com[/mailto]
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment