本文實例講述了WordPress實現(xiàn)評論后可顯示內(nèi)容中附件下載地址的方法。分享給大家供大家參考,具體如下:
最近在做一個項目的時候,有個需求就是希望WordPress網(wǎng)站文章內(nèi)容里面附件可以評論后才可以下載。網(wǎng)絡(luò)上面查了會,發(fā)現(xiàn)這個功能不難實現(xiàn),寫個簡單的函數(shù)就可以了。而且這樣也可以設(shè)置部分文章評論后可見。覺得這個功能應(yīng)該挺多人有需要的,索性也就寫一篇wordpress文章內(nèi)容回復(fù)后可見的教程。現(xiàn)在來說說如何實現(xiàn)wordpress的文章內(nèi)容評論后可見吧?其實實現(xiàn)起來很簡單,利用wordpress的短代碼功能即可實現(xiàn),代碼如下:
復(fù)制代碼
代碼如下:function reply_to_read($atts, $content=null) {
extract(shortcode_atts(array("notice" => '溫馨提示: 此處內(nèi)容需要評論本文后才能查看.'), $atts));
$email = null;
$user_ID = (int) wp_get_current_user()->ID;
if ($user_ID > 0) {
$email = get_userdata($user_ID)->user_email;
//對博主直接顯示內(nèi)容
$admin_email = "xxx@aaa.com"; //博主Email
if ($email == $admin_email) {
return $content;
}
} else if (isset($_COOKIE['comment_author_email_' . COOKIEHASH])) {
$email = str_replace('%40', '@', $_COOKIE['comment_author_email_' . COOKIEHASH]);
} else {
return $notice;
}
if (empty($email)) {
return $notice;
}
global $wpdb;
$post_id = get_the_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 do_shortcode($content);
} else {
return $notice;
}
}
add_shortcode('reply', 'reply_to_read');
extract(shortcode_atts(array("notice" => '溫馨提示: 此處內(nèi)容需要評論本文后才能查看.'), $atts));
$email = null;
$user_ID = (int) wp_get_current_user()->ID;
if ($user_ID > 0) {
$email = get_userdata($user_ID)->user_email;
//對博主直接顯示內(nèi)容
$admin_email = "xxx@aaa.com"; //博主Email
if ($email == $admin_email) {
return $content;
}
} else if (isset($_COOKIE['comment_author_email_' . COOKIEHASH])) {
$email = str_replace('%40', '@', $_COOKIE['comment_author_email_' . COOKIEHASH]);
} else {
return $notice;
}
if (empty($email)) {
return $notice;
}
global $wpdb;
$post_id = get_the_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 do_shortcode($content);
} else {
return $notice;
}
}
add_shortcode('reply', 'reply_to_read');
1.需要注意的是,要修改第8行的郵件為管理員的。如果你的網(wǎng)站使用了ajax免刷新提交評論,應(yīng)該還需要修改第2行的提示文字,提示訪客評論后刷新頁面來查看隱藏內(nèi)容。
2.編輯文章時,使用下面的簡碼:
【reply】評論可見的內(nèi)容【/reply】
或者
【reply notice="自定義的提示信息"】評論可見的內(nèi)容【/reply】
希望本文所述對大家基于wordpress的程序設(shè)計有所幫助。
【網(wǎng)站聲明】本站除付費源碼經(jīng)過測試外,其他素材未做測試,不保證完整性,網(wǎng)站上部分源碼僅限學(xué)習(xí)交流,請勿用于商業(yè)用途。如損害你的權(quán)益請聯(lián)系客服QQ:2655101040 給予處理,謝謝支持。