antispambot( string $email_address, int $hex_encoding )

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


Description Description


Parameters Parameters

$email_address

(string) (Required) Email address.

$hex_encoding

(int) (Optional) Set to 1 to enable hex encoding.


Top ↑

Return Return

(string) Converted email address.


Top ↑

Source Source

File: wp-includes/formatting.php

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 ( $j == 0 ) {
			$email_no_spam_address .= '&#' . ord( $email_address[ $i ] ) . ';';
		} elseif ( $j == 1 ) {
			$email_no_spam_address .= $email_address[ $i ];
		} elseif ( $j == 2 ) {
			$email_no_spam_address .= '%' . zeroise( dechex( ord( $email_address[ $i ] ) ), 2 );
		}
	}

	return str_replace( '@', '&#64;', $email_no_spam_address );
}

Top ↑

Changelog Changelog

Changelog
Version Description
0.71 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    Example

    /**
     * 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 wpdocs_hide_email_shortcode( $atts , $content = null ) {
    	if ( ! is_email( $content ) ) {
    		return;
    	}
    	return '<a href="mailto:' . esc_url( antispambot( $content ) ) . '">' . esc_html( antispambot( $content ) ) . '</a>';
    }
    add_shortcode( 'email', 'wpdocs_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' );
    
    

You must log in before being able to contribute a note or feedback.