WordPress.org

Ready to get started?Download WordPress

Codex

Function Reference/antispambot

Contents

Description

Converts email addresses characters to HTML entities to block spam bots.

Usage

<?php antispambot$emailaddy$hex_encoding ?>

Parameters

$emailaddy
(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;
	}

	return '<a href="mailto:' . antispambot( $content ) . '">' . antispambot( $content ) . '</a>';
}
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 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;

And in their browser, people will still see it right:

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.