最新消息:

WordPress 隐藏部分评论用户名

很多小伙伴的网站开启了评论功能,也有很多优化了或者美化了此项功能,使之更加亲民更加美观。如果你想给它加入一些保护隐私的功能,例如隐藏部分评论用户名。那么请复制以下代码到当前使用主题的 functions.php 文件中即可。

/*
 功能描述: 隐藏部分评论用户名
 功能介绍页面: https://www.ivicos.com/69.html
*/
function cut_str ($string, $sublen, $start = 0, $code = 'UTF-8') {
 if ($code == 'UTF-8') {
 $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
 preg_match_all($pa, $string, $t_string);
 if (count($t_string[0]) - $start > $sublen) {
 return join('', array_slice($t_string[0], $start, $sublen));
 }
 return join('', array_slice($t_string[0], $start, $sublen));
 } else {
 $start = $start*2;
 $sublen = $sublen*2;
 $strlen = strlen($string);
 $tmpstr = '';
 for ($i=0; $i< $strlen; $i++) {
 if ($i>=$start && $i< ($start+$sublen)) {
 if(ord(substr($string, $i, 1))>129) {
 $tmpstr.= substr($string, $i, 2);
 } else {
 $tmpstr.= substr($string, $i, 1);
 }
 }
 if (ord(substr($string, $i, 1))>129) {
 $i++;
 }
 }
 return $tmpstr;
 }
}

function my_get_comment_author_link() {
 $url = get_comment_author_url($comment_ID);
 $author = cut_str(get_comment_author($comment_ID), 2, 0).'***'.cut_str(get_comment_author($comment_ID), 1, -1);
 if (empty($url) || 'http://' == $url) {
 return $author;
 } else {
 return "<a target='_blank' href='$url' rel='external nofollow' class='url'>$author</a>";
 }
}
add_filter('get_comment_author_link', 'my_get_comment_author_link');

好了,WordPress 隐藏部分评论用户名 的方法就分享到这里,非常感谢你的来访。如果你喜欢本站,请不要忘记收藏本站,以便下次继续访问;也可以 关注站长微博 随时获取最新动态。你的支持就是我最大的动力!

转载请注明:爱维科斯 » WordPress 隐藏部分评论用户名

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章对你有帮助,欢迎点击上方按钮打赏作者