antispambot() WordPress Function

The antispambot() function is a built-in function in WordPress that helps to protect email addresses from being spammed. It encodes email addresses using the ROT13 encoding method and replaces certain characters with their HTML entity equivalents. This function can be used when displaying email addresses in blog posts or in comments.

antispambot( string $email_address, int $hex_encoding ) #

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


Parameters

$email_address

(string)(Required)Email address.

$hex_encoding

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


Top ↑

Return

(string) Converted email address.


Top ↑

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 ( 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( '@', '&#64;', $email_no_spam_address );
}


Top ↑

Changelog

Changelog
VersionDescription
0.71Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More