Languages: English • Italiano • Português do Brasil • (Add your language)
Converts selected email addresses characters to HTML entities to block spam bots. Not all characters in the email address are converted: the selection is random and changes each time the function is called.
<?php antispambot( $email_address, $hex_encoding ) ?>
/** * Hide email from Spam Bots using a shortcode. * * @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 ) { if ( ! is_email( $content ) ) { return; } $content = antispambot( $content ); $email_link = sprintf( 'mailto:%s', $content ); return sprintf( '<a href="%s">%s</a>', esc_url( $email_link, array( 'mailto' ) ), esc_html( $content ) ); } add_shortcode( 'email', 'wpcodex_hide_email_shortcode' );
To use this in your WordPress Content area all you have to do it wrap it in a short code.
[email]john.doe@mysite.com[/email]
You can also use this in a plain text widget if you add this filter to your function file as well.
add_filter( 'widget_text', 'shortcode_unautop' ); add_filter( 'widget_text', 'do_shortcode' );
<?php echo esc_html( antispambot( 'john.doe@mysite.com' ) ); ?>
This will output the email like this in the HTML:
john.doe@mysite.com
But it will appear as a normal email address to anyone using a web browser:
john.doe@mysite.com
Since: 0.71
antispambot() is located in wp-includes/formatting.php
.