WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/antispambot

Description

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.

Usage

<?php antispambot$email_address$hex_encoding ?>

Parameters

$email_address
(string) (required) Email address.
Default: None
$hex_encoding
(integer) (optional) 0 or 1. Use 0 to only allow decimal encoding (&#123;) and 1 to also allow hex encoding (&x7B;).
Default: 0

Return Values

(string) 
Converted email address.

Examples

/**
 * 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' );

Default Usage

<?php
echo esc_html( antispambot( 'john.doe@mysite.com' ) );
?>

This will output the email like this in the HTML:

&#106;&#111;h&#110;&#46;&#100;&#111;&#101;&#64;mysit&#101;.&#99;&#111;&#109;

But it will appear as a normal email address to anyone using a web browser:

john.doe@mysite.com

Notes

Change Log

Since: 0.71

Source File

antispambot() is located in wp-includes/formatting.php.

Related

See also index of Function Reference and index of Template Tags.