WordPress评论框输入QQ一键获取头像和昵称

看到很多朋友的WordPress博客留言输入QQ可以获取到QQ头像和QQ昵称,我觉得特别的有意思,于是在网上搜了搜相关的WordPress教程,相关的内容挺多的。

为了以后方便使用,先整理记录下来。具体的效果就如下图所示:

步骤一:插入QQ字段到评论表单

首先我们在正在使用的WordPress主题评论页面文件找到昵称、邮箱、网址等表单结构依样添加 input 标签,比如我的表单结构是:

<code><p class="comment-form-author"<span class="xiomao-483-mypl" data-mark="seo"></span>>
<label>昵称</label>
<input id="yuao-comt-author" name="author"<span class="xiomao-567-mypl" data-mark="seo"></span> type="text" value="<?php echo esc_attr($comment_author); ?>" maxlength="245"<span class="xiomao-656-mypl" data-mark="seo"></span> required="required" placeholder="必填" />
</p></code>

添加QQ照着复制就行了,昵称等文本改成QQ,注意name属性值要改成new_field_qq,下面传参要用到。

前端代码加了input,后端就要添加字段用来存储前端提交到数据库的QQ信息

下方代码请添加到主题中functions.php文件:

<code>// 数据库插入评论表单的qq字段
add_act<span class="xiomao-960-mypl" data-mark="seo"></span>ion('wp_insert_comment','inlojv_sql_insert_qq_field',10,2);
function i<span class="xiomao-1034-mypl" data-mark="seo"></span>nlojv_sql_insert_qq_<span class="xiomao-1054-mypl" data-mark="seo"></span>field(<span class="xiomao-1060-mypl" data-mark="seo"></span>$c<span class="xiomao-1062-mypl" data-mark="seo"></span>omment_ID,$<span class="xiomao-1073-mypl" data-mark="seo"></span>commmentdata) {
$qq = isset($_POST['new_field_qq']) ? $_POST['new_field_qq<span class="xiomao-1150-mypl" data-mark="seo"></span>'<span class="xiomao-1152-mypl" data-mark="seo"></span>] : false;
update_comment_<span class="xiomao-1178-mypl" data-mark="seo"></span>meta($comment_ID,'new_field_qq<span class="xiomao-1210-mypl" data-mark="seo"></span>',$qq); // new_field_qq 是表单nam<span class="xiomao-1240-mypl" data-mark="seo"></span>e值,也是存储<span class="xiomao-1247-mypl" data-mark="seo"></span>在数据库里的字段名字
}
// 后台评论中显示qq字段
add_filter( 'manage<span class="xiomao-1295-mypl" data-mark="seo"></span>_edit-comments_columns',<span class="xiomao-1320-mypl" data-mark="seo"></span> 'add_commen<span class="xiomao-1333-mypl" data-mark="seo"></span>ts_columns' );
add_action( 'manage_comments_custom_column', 'output_comments_qq_columns', 10, 2 );
function add_comments_columns( $columns ){
$columns[ 'new_field_qq' ] = __( 'QQ号' );        // 新增列名称
return $columns;
}<span class="xiomao-1560-mypl" data-mark="seo"></span>
funct<span class="xiomao-1566-mypl" data-mark="seo"></span>ion output_comments_qq_columns( $column_name, $comment_id ){
switch( $column_name ) {
case "new_field_qq" :
// 这是输出值,可以拿来在前端输出,这里已经在钩子manage_comments_custom_col<span class="xiomao-1728-mypl" data-mark="seo"></span>umn上输出了
echo get_comment_meta( $comment_id, 'ne<span class="xiomao-1776-mypl" data-mark="seo"></span>w_field_qq', true );
break;
}
}
</code>

步骤二:输入qq号实时获取昵称和头像

然后,评论者在QQ表单填入qq号之后,ajax会向后端发送get请求,将qq号传给后端API文件进行处理,处理完毕再把信息返回到前端,依靠js把信息(昵称和头像)输出到前端评论表单上。
而当访客评论完毕,刷新页面时,昵称和头像显示在评论表单上,这时候就要用到cookie操作。

下方为前端处理的js代码(主题需要jQuery支持):

<code>// 初始化
$(func<span class="xiomao-2112-mypl" data-mark="seo"></span>tion(){
inlojv_js_getqqinfo();
});
// 设置cookie
function se<span class="xiomao-2170-mypl" data-mark="seo"></span>tCookie(a,c){var b=30;var d=new Da<span class="xiomao-2204-mypl" data-mark="seo"></span>te();d.setTime(d.ge<span class="xiomao-2223-mypl" data-mark="seo"></span>tTime()+b*24*60*60*1000);docume<span class="xiomao-2254-mypl" data-mark="seo"></span>nt.cookie=a+"<span class="xiomao-2268-mypl" data-mark="seo"></span>="+escape(c)+";expires="<span class="xiomao-2295-mypl" data-mark="seo"></span>+d.toGMTString()}
// 获取cookie
function getCookie(b){var a,c=new RegExp("(^| )"+b+"=([^;]*)(;|$)");if(a=document.cookie.match(c)){return unescape(a[2])}else{return null}}
// 核心函数
function inlojv_js_getqqinfo(){
// 获取cookie
if(getCookie(<span class="xiomao-2535-mypl" data-mark="seo"></span>'user_avatar')<span class="xiomao-2550-mypl" data-mark="seo"></span> && getCookie('user_qq') ){
$('<span class="xiomao-2592-mypl" data-mark="seo"></span>div.comment-user-avatar img').attr('src',getCookie('user_avatar'));
$('#yuao-comt-qq').val(getCookie(<span class="xiomao-2700-mypl" data-mark="seo"></span>'user_qq'));
}
$('#yuao-c<span class="xiomao-2728-mypl" data-mark="seo"></span>omt-qq').on('blur',functi<span class="xiomao-2756-mypl" data-mark="seo"></span>on(){
var qq=$('#yuao-comt-qq').val(); // 获取访客填在qq表单上的qq数字<span class="xiomao-2816-mypl" data-mark="seo"></span>,其中#yuao-comt-qq表示QQ input标签上的id,改成你自己的!
$('#yuao-comt-email')<span class="xiomao-2880-mypl" data-mark="seo"></span>.val($.trim(qq)+'@qq.com'); //<span class="xiomao-2912-mypl" data-mark="seo"></span> 将<span class="xiomao-2914-mypl" data-mark="seo"></span>获取到的qq,改成qq邮箱填入邮箱表单,其中#yuao-comt-email表示邮箱in<span class="xiomao-2958-mypl" data-mark="seo"></span>put标签上的id,改成你自己的!
// ajax方法获取昵称
$.ajax({
type: 'get',
url:'http://127.0.0.1<span class="xiomao-3036-mypl" data-mark="seo"></span>/func_getqqinfo.php?type<span class="xiomao-3060-mypl" data-mark="seo"></span>=getqqnickname&qq='+qq<span class="xiomao-3087-mypl" data-mark="seo"></span>,  // func_getqqin<span class="xiomao-3105-mypl" data-mark="seo"></span>fo.php是后端处理文件,注<span class="xiomao-3120-mypl" data-mark="seo"></span>意路径,127.0.0.<span class="xiomao-3132-mypl" data-mark="seo"></span>1 改成<span class="xiomao-3136-mypl" data-mark="seo"></span>你自己的域名
dataTyp<span class="xiomao-3150-mypl" data-mark="seo"></span>e: 'jsonp',
jsonp: 'callback',
jsonpCallback: 'portraitCallBack',
success: function(data)<span class="xiomao-3245-mypl" data-mark="seo"></span> {
// console.log(data<span class="xiomao-3267-mypl" data-mark="seo"></span>);
$('#yuao-comt-author').val(data[qq][6]);	// 将返回的qq昵称填入到昵称input表单上,其中#yuao-comt-author表示昵称in<span class="xiomao-3363-mypl" data-mark="seo"></span>put标签上的id,改成你自己的!
alert('已获取昵称!'); //<span class="xiomao-3402-mypl" data-mark="seo"></span> 弹出警告
setCooki<span class="xiomao-3416-mypl" data-mark="seo"></span>e('us<span class="xiomao-3422-mypl" data-mark="seo"></span>er_qq',qq);	// 设置cookie
},
error: function() {
$('#y<span class="xiomao-3476-mypl" data-mark="seo"></span>uao-comt-qq,#yuao-comt-author,#yua<span class="xiomao-3510-mypl" data-mark="seo"></span>o-comt-email').val(''); /<span class="xiomao-3538-mypl" data-mark="seo"></span>/ 如果获取失败则清空表单,<span class="xiomao-3552-mypl" data-mark="seo"></span>注意input标签上的id<span class="xiomao-3565-mypl" data-mark="seo"></span>,改成你自己的!
alert('糟糕<span class="xiomao-3584-mypl" data-mark="seo"></span>,昵称获取失败!请重新填写。'); // 弹出警告
}
});
// 获<span class="xiomao-3621-mypl" data-mark="seo"></span>取头像
$.ajax({
type: 'get',
url: 'http://127.0.0.1<span class="xiomao-3672-mypl" data-mark="seo"></span>/func_getqqinfo.php?type<span class="xiomao-3696-mypl" data-mark="seo"></span>=getqqavatar&qq='+qq, // func_getqqinfo.php是后端处理文件,注意路径,127.0.0.1 改成你自己的域名!
dataType: 'jsonp',
jsonp: 'callback',
jsonpCall<span class="xiomao-3828-mypl" data-mark="seo"></span>back: 'qqavatarCallBack<span class="xiomao-3852-mypl" data-mark="seo"></span>',
success: function(data) {
$('div.comment-user-avatar img').attr('src',data[qq]);	// 将返回的qq头像设置到你评论表单<span class="xiomao-3960-mypl" data-mark="seo"></span>区域显示头像的节点<span class="xiomao-3969-mypl" data-mark="seo"></span>上,div.c<span class="xiomao-3976-mypl" data-mark="seo"></span>omment-user-avatar img表示头像节点img标签,改成<span class="xiomao-4012-mypl" data-mark="seo"></span>你自己的!
alert('已获取头像!'); // 弹出警告
setCookie('user_avatar<span class="xiomao-4068-mypl" data-mark="seo"></span>',data[qq]);	 // 设置<span class="xiomao-4088-mypl" data-mark="seo"></span>cookie
}<span class="xiomao-4096-mypl" data-mark="seo"></span>,
error: function() {
alert('糟糕,获取头像失败了!请重新填写。'); // 弹出警告
$(<span class="xiomao-4158-mypl" data-mark="seo"></span>'#yuao-comt-qq,#yuao-comt-author,#yuao-comt-email').val(''); //<span class="xiomao-4225-mypl" data-mark="seo"></span> 清空表单
}
});
});
}</code>

下面是后端处理的php代码(需用到两个api),新建一个func_getqqinfo.php文件,将文件放到网站根目录与上面ajax请求的路径要一致!

<code><?p<span class="xiomao-4408-mypl" data-mark="seo"></span>hp
header("content-Type: text/html; charset=utf-8");
$type =<span class="xiomao-4470-mypl" data-mark="seo"></span> @$_GET['type'] ? $_GET['type'] : '';
if(empty($ty<span class="xiomao-4526-mypl" data-mark="seo"></span>pe)){
//header("Location:http://w<span class="xiomao-4560-mypl" data-mark="seo"></span>ww.wpToo.com/");
exit;
}
if($type == "getqqnickname"){
$qq = isset($_GET['qq']) ? addslashes(trim($_GET['qq'])) : '';
if(!empty($qq) &a<span class="xiomao-4704-mypl" data-mark="seo"></span>mp;& is_numeric($qq) && strlen($qq) > 4 && strlen($qq) < 13){
$qqnickname = file_get_contents('http://users.qzone.qq.com/fcg-bin/cgi_get_portrait.fcg?uins='.$qq); // API
if($qqnickname){
$qqnickname = mb_convert_encoding($qqni<span class="xiomao-4958-mypl" data-mark="seo"></span>ckname, "<span class="xiomao-4968-mypl" data-mark="seo"></span>UTF-8", "GBK");
e<span class="xiomao-4988-mypl" data-mark="seo"></span>cho $qqnickname;
}
}
}
if(<span class="xiomao-5014-mypl" data-mark="seo"></span>$type == "getqqavatar"){
$qq = isset($_GET['qq']) ? addsla<span class="xiomao-5076-mypl" data-mark="seo"></span>shes(tri<span class="xiomao-5084-mypl" data-mark="seo"></span>m($_GET['qq'])) : '';
if(!empty($qq) && is_numeric($<span class="xiomao-5148-mypl" data-mark="seo"></span>qq) && strlen($qq) > 4 &<span class="xiomao-5183-mypl" data-mark="seo"></span>amp;& strlen($qq) &lt<span class="xiomao-5208-mypl" data-mark="seo"></span>; 13){
$qqav<span class="xiomao-5220-mypl" data-mark="seo"></span>atar = file_get_contents('htt<span class="xiomao-5250-mypl" data-mark="seo"></span>p://ptlogin2.qq.com/getface?appid=1006102&imgtype=3&uin='.$qq);  // API
if($qqavat<span class="xiomao-5341-mypl" data-mark="seo"></span>ar){
echo str_replace(<span class="xiomao-5363-mypl" data-mark="seo"></span>"pt.setHeader","qqavatarCallBack",$qqav<span class="xiomao-5406-mypl" data-mark="seo"></span>atar);
}
}
}</code>

步骤三:让QQ头像显示在评论列表和后台评论上

一般情况下,WordPress的评论头像默认是gravatar,所以我们用get_avatar过滤器来对默认头像的调用方式进行改造。以下代码加入到主题的functions.php里

<code>/**<span class="xiomao-5628-mypl" data-mark="seo"></span>
* 修改后台头像
* 若有qq字段则显示qq头像,若没有则显示gravatar
*/
add_filter( 'get_avatar', 'inlojv_change_avatar'<span class="xiomao-5724-mypl" data-mark="seo"></span>, 10, 3 );
function inlojv<span class="xiomao-5750-mypl" data-mark="seo"></span>_change_avatar<span class="xiomao-5764-mypl" data-mark="seo"></span>($avatar){
global $comment;
if( get_comment_meta( $comment->comment_ID, 'new_field<span class="xiomao-5850-mypl" data-mark="seo"></span>_qq', true ) ){
$qq_number =  get_comment_meta( $comment->comment_ID, 'ne<span class="xiomao-5928-mypl" data-mark="seo"></span>w_field_qq', true );
$qqavatar = file_get_contents('http://ptlogin2.qq.com/getface?appid=1<span class="xiomao-6020-mypl" data-mark="seo"></span>006102<span class="xiomao-6026-mypl" data-mark="seo"></span>&imgtype=3&a<span class="xiomao-6042-mypl" data-mark="seo"></span>mp;uin<span class="xiomao-6048-mypl" data-mark="seo"></span>='.$qq_number);
preg_match('/http:(.*?)&t/',$qqavata<span class="xiomao-6107-mypl" data-mark="seo"></span>r,$m); // 匹配 http: 和 &t 之<span class="xiomao-6136-mypl" data-mark="seo"></span>间的字符串
return '<img src="'.stripslashes($m[1]).<span class="xiomao-6188-mypl" data-mark="seo"></span>'" cl<span class="xiomao-6195-mypl" data-mark="seo"></span>ass="avatar avatar-40 phot<span class="xiomao-6222-mypl" data-mark="seo"></span>o" wi<span class="xiomao-6228-mypl" data-mark="seo"></span>dth="40" h<span class="xiomao-6240-mypl" data-mark="seo"></span>eight="40"  alt="qq_a<span class="xiomao-6264-mypl" data-mark="seo"></span>vatar" />';
}else{
return $avatar ;
}
}</code>

WordPress评论获取QQ头像和昵称教程,到这里基本就完成了,如果你有一定的代码基础,也可以对js的处理部分进行与主题相匹配的改造。

评论输入QQ一键获取头像和昵称的功能到此就完成了,大家可以自己动手试试。

该文章转载来自暖岛网

人已赞赏
wordpress教程网站教程

WrodPress获取当前用户/作者文章总浏览数量

2020-10-28 22:02:05

wordpress教程网站教程

WordPress调用指定数量最新发布文章

2020-10-28 22:02:10

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索