wordpress功能函数antispambot(),转换电子邮件地址字符HTML实体,以阻止垃圾邮件机器人。

用法:

antispambot( string $email_address, int $hex_encoding )

参数:

$email_address

(string) (必需) 电子邮件地址。

$hex_encoding

(int) (可选) 设置为1表示启用十六进制编码。

返回

(string)转换后的电子邮件地址。

来源

文件: 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( '@', '@', $email_no_spam_address );

}

更新日志:
WordPress功能函数antispambot() (https://www.wpzt.net/) WordPress开发教程 第1张
用户贡献的笔记

(由Codex - 6年前贡献)

例子

/**

* 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 '' . esc_html( antispambot( $content ) ) . '';

}

add_shortcode( 'email', 'wpdocs_hide_email_shortcode' );

要在你的WordPress内容区使用它,你需要做的就是用一个简短的代码包装它。

[email]john.doe@mysite.com[/email]

如果将这个过滤器添加到函数文件中,还可以在纯文本小部件中使用它。

add_filter( 'widget_text', 'shortcode_unautop' );

add_filter( 'widget_text', 'do_shortcode' );

由@johnrafferty编辑