几乎每个人都会遭到垃圾邮件的困扰,其实很多情况下,是由于我们的邮箱地址被恶意采集造成的,如果你使用WordPress建站,那你可以使用一段简单的代码让WordPress转义文章和评论中的邮箱地址,以防被恶意采集。
只需要在wordpress主题的 functions.php 的最后一个 ?> 前添加下面的代码即可:
function security_remove_emails($content) {
$pattern = '/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4})/i';
$fix = preg_replace_callback($pattern,"security_remove_emails_logic", $content);
return $fix;
}
function security_remove_emails_logic($result) {
return antispambot($result[1]);
}
add_filter( 'the_content', 'security_remove_emails', 20 );
add_filter( 'widget_text', 'security_remove_emails', 20 ); |
function security_remove_emails($content) {
$pattern = '/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4})/i';
$fix = preg_replace_callback($pattern,"security_remove_emails_logic", $content);
return $fix;
}
function security_remove_emails_logic($result) {
return antispambot($result[1]);
}
add_filter( 'the_content', 'security_remove_emails', 20 );
add_filter( 'widget_text', 'security_remove_emails', 20 );
该代码是通过wordpress的antispambot函数来转义邮箱地址的,添加以上代码后,你可以尝试你的某篇文章中输入一个Email地址,更新后查看这篇文章的源代码,就会看到转义后的邮箱地址。
如Email地址:john@a.com
转义后,源代码中看到的是:john@a.com
邮箱地址采集器都是通过源代码来采集Email地址的,转义后的内容对它们来说几乎是无法识别的。虽然在源代码中你会看到一堆乱码,但是你的文章和评论中,我们还是可以看到正常的Email地址,并可以自由地复制的。
本文参考自:How to automatically hide email adresses from spambots on your WordPress blog 以及 露兜博客。
来源:
https://www.wpdaxue.com/security-remove-emails.html
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_33808.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57