Skip to content

Instantly share code, notes, and snippets.

@agiannis
Last active August 7, 2023 08:17
Show Gist options
  • Save agiannis/ef9bca877d7911a44081 to your computer and use it in GitHub Desktop.
Save agiannis/ef9bca877d7911a44081 to your computer and use it in GitHub Desktop.
label: wordpress, email , php , shortcode , hide
/**
* Protect an email address from harvesting
* based on http://www.maurits.vdschee.nl/php_hide_email/
*
* @param string $email email address to protect
* @param string $text (optional) The text of the link.
* @param string $extra (optional) Any extra parameters on the <a> tag
* @param string $subject (optional) Subject of the email
* @return string the link
*/
function hide_email($email, $text = '', $extra = '', $subject = '')
{
$character_set = '+-.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
$key = str_shuffle($character_set);
$cipher_text = '';
$id = 'e' . rand(1, 999999999);
for ($i = 0; $i < strlen($email); $i += 1) $cipher_text .= $key[strpos($character_set, $email[$i])];
if ($email == $text) {
$text = '"+d+"';
}
$script = 'var a="' . $key . '";var b=a.split("").sort().join("");var c="' . $cipher_text . '";var d="";';
$script .= 'for(var e=0;e<c.length;e++)d+=b.charAt(a.indexOf(c.charAt(e)));';
$script .= 'document.getElementById("' . $id . '").innerHTML="<a href=\\"mailto:"+d+"?subject=' . $subject . '\\" ' . $extra . ' >' . $text . '</a>"';
$script = "eval(\"" . str_replace(array("\\", '"'), array("\\\\", '\"'), $script) . "\")";
$script = '<script type="text/javascript">/*<![CDATA[*/' . $script . '/*]]>*/</script>';
return '<span id="' . $id . '">[javascript protected email address]</span>' . $script;
}
function shortcode_hide_email($atts)
{
$a = shortcode_atts(array(
'email' => '',
'text' => $atts['email'],
'extra' => $atts['extra'],
'subject' => ''
), $atts);
/** @var string $email */
/** @var string $subject */
/** @var string $extra */
/** @var string $text */
extract($a);
if (!empty($subject)) {
$subject = '?subject='.$subject;
}
$character_set = '+-.0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
$key = str_shuffle($character_set);
$cipher_text = '';
$id = 'e' . rand(1, 999999999);
for ($i = 0; $i < strlen($email); $i += 1) $cipher_text .= $key[strpos($character_set, $email[$i])];
if ($email == $text) {
$text = '"+d+"';
}
$script = 'var a="' . $key . '";var b=a.split("").sort().join("");var c="' . $cipher_text . '";var d="";';
$script .= 'for(var e=0;e<c.length;e++)d+=b.charAt(a.indexOf(c.charAt(e)));';
$script .= 'document.getElementById("' . $id . '").innerHTML="<a href=\\"mailto:"+d+"' . $subject . '\\" ' . $extra . ' >' . $text . '</a>"';
$script = "eval(\"" . str_replace(array("\\", '"'), array("\\\\", '\"'), $script) . "\")";
$script = '<script type="text/javascript">/*<![CDATA[*/' . $script . '/*]]>*/</script>';
return '<span id="' . $id . '">[javascript protected email address]</span>' . $script;
}
add_shortcode('hide_email', 'shortcode_hide_email');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment