返回介绍

antispambot()

发布于 2017-09-10 21:29:08 字数 2914 浏览 1235 评论 0 收藏 0

antispambot( string $email_address,  int $hex_encoding )

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


description


参数

$email_address

(string) (Required) Email address.

$hex_encoding

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


返回值

(string) Converted email address.


源代码

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

更新日志

Versiondescription
0.71Introduced.

相关函数

Uses

  • wp-includes/formatting.php: zeroise()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note 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:' . antispambot( $content ) . '">' . 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' );
    
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文