WordPress功能函數(shù)antispambot()
wordpress功能函數(shù)antispambot(),轉(zhuǎn)換電子郵件地址字符HTML實(shí)體,以阻止垃圾郵件機(jī)器人。
用法:
antispambot( string $email_address, int $hex_encoding )
參數(shù):
$email_address
(string) (必需) 電子郵件地址。
$hex_encoding
(int) (可選) 設(shè)置為1表示啟用十六進(jìn)制編碼。
返回
(string)轉(zhuǎn)換后的電子郵件地址。
來源
文件: 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 );
}
更新日志:

用戶貢獻(xiàn)的筆記
(由Codex - 6年前貢獻(xiàn))
例子
/**
* 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內(nèi)容區(qū)使用它,你需要做的就是用一個(gè)簡短的代碼包裝它。
[email]john.doe@mysite.com[/email]
如果將這個(gè)過濾器添加到函數(shù)文件中,還可以在純文本小部件中使用它。
add_filter( 'widget_text', 'shortcode_unautop' );
add_filter( 'widget_text', 'do_shortcode' );
由@johnrafferty編輯