Converts email addresses characters to HTML entities to block spam bots.
Parameters
$email_address
stringrequired- Email address.
$hex_encoding
intoptional- Set to 1 to enable hex encoding.
Source
function antispambot( $email_address, $hex_encoding = 0 ) {
$email_no_spam_address = '';
for ( $i = 0, $len = strlen( $email_address ); $i < $len; $i++ ) {
$j = rand( 0, 1 + $hex_encoding );
if ( 0 === $j ) {
$email_no_spam_address .= '&#' . ord( $email_address[ $i ] ) . ';';
} elseif ( 1 === $j ) {
$email_no_spam_address .= $email_address[ $i ];
} elseif ( 2 === $j ) {
$email_no_spam_address .= '%' . zeroise( dechex( ord( $email_address[ $i ] ) ), 2 );
}
}
return str_replace( '@', '@', $email_no_spam_address );
}
Changelog
Version | Description |
---|---|
0.71 | Introduced. |
Example
To use this in your WordPress Content area all you have to do it wrap it in a short code.
You can also use this in a plain text widget if you add this filter to your function file as well.
Edited with a contribution from @johnrafferty
http://
before email address. In lack of better option, it’s more appropriate to useesc_attr()
instead.widget_text
is no longer necessary as shortcodes are allowed in text widgets by default.I really feel like this function should be updated. We’re in 2024 and HTML entities can be easily decoded by spam bots. At this point, this technique does not block spam at all.
If you can, try to use Xor on your WordPress site; avoid using this function.