level 14
PC丶爱好者
楼主
我们经常在各种论坛上看到“回复后可见隐藏内容”的字样,这样既保证了帖子能让更多人看到,也可以防止贴文被恶意转载。那么在WordPress上如何实现这样效果呢?一起来看看吧。
还是建议大家少用这样的功能。
在functions.php中插入如下代码:请注意修改其中的Email地址哦!
function reply_to_read($atts,$content=null){
extract(shortcode_atts(array("notice"=>*<span class="reply-to-read">此处内容需要<a href="*. get_permalink().*#respond" title="评论本文">评论本文</a>后<a href="javascript:window.location.reload();" title="刷新">刷新本页</a>才能查看.</span>*),$atts));
$email=null;
$user_ID=(int)wp_get_current_user()->ID;
if($user_ID>0){
$email = get_userdata($user_ID)->user_email; //如果用户已登录,从登录信息中获取email
}else if(isset($_COOKIE[*comment_author_email_*.COOKIEHASH])){
$email=str_replace(*%40*,*@*,$_COOKIE[*comment_author_email_*.COOKIEHASH]); //如果用户未登录但电脑上有本站的Cookie信息,从Cookie里读取email
}else{
return $notice; //无法获取email,直接返回提示信息
}
if(empty($email)){
return $notice;
}
global $wpdb;
$post_id=get_the_ID(); //文章的ID
$query="SELECT `comment_ID` FROM {$wpdb->comments} WHERE `comment_post_ID`={$post_id} and `comment_approved`=*1* and `comment_author_email`=*{$email}* LIMIT 1";
if($wpdb->get_results($query)){
return $content; //查询到对应的已经审核通过的评论则返回内容
}else{
return $notice; //否则返回提示信息
}
}
add_shortcode(*reply*, *reply_to_read*);
2014年09月13日 12点09分
1
还是建议大家少用这样的功能。
在functions.php中插入如下代码:请注意修改其中的Email地址哦!
function reply_to_read($atts,$content=null){
extract(shortcode_atts(array("notice"=>*<span class="reply-to-read">此处内容需要<a href="*. get_permalink().*#respond" title="评论本文">评论本文</a>后<a href="javascript:window.location.reload();" title="刷新">刷新本页</a>才能查看.</span>*),$atts));
$email=null;
$user_ID=(int)wp_get_current_user()->ID;
if($user_ID>0){
$email = get_userdata($user_ID)->user_email; //如果用户已登录,从登录信息中获取email
}else if(isset($_COOKIE[*comment_author_email_*.COOKIEHASH])){
$email=str_replace(*%40*,*@*,$_COOKIE[*comment_author_email_*.COOKIEHASH]); //如果用户未登录但电脑上有本站的Cookie信息,从Cookie里读取email
}else{
return $notice; //无法获取email,直接返回提示信息
}
if(empty($email)){
return $notice;
}
global $wpdb;
$post_id=get_the_ID(); //文章的ID
$query="SELECT `comment_ID` FROM {$wpdb->comments} WHERE `comment_post_ID`={$post_id} and `comment_approved`=*1* and `comment_author_email`=*{$email}* LIMIT 1";
if($wpdb->get_results($query)){
return $content; //查询到对应的已经审核通过的评论则返回内容
}else{
return $notice; //否则返回提示信息
}
}
add_shortcode(*reply*, *reply_to_read*);