日志标签:wp评论后可见

wordpress文章评论后可见

分类:折腾日期:2012-02-27 - 18:15:33评论:27条作者:老谢

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');

以上代码复制到function.php即可,用法如下:

[reply]评论可见内容[/reply]

原文传送门:http://zuidongting.com/non-plug-ins-to-achieve-the-contents-of-the-article-shows-the-code-reply-share.html