无插件实现WordPress内容回复评论才可以见效果

来自:互联网
时间:2019-09-01
阅读:

经常看到有些网友的网站,内容是隐藏的,需要留言评论之后才可以看到,比如回复可以见到邀请码、下载链接等等。这样的效果是如何做的呢?因为在一定程度上可以实现提高网友与网站的互动,增加评论的人气,只要能提供有用的内容,用户还是希望回复的。

这里,麦子整理到网上无插件实现WORDPRESS回复可以见的效果,我们就一起看看如何实现的,且如果我们网站有需要用到可以采用。

第一、添加代码到functions.php

<?php
function reply_to_read($atts, $content=null) {
extract(shortcode_atts(array("notice" => '<p class="reply-to-read" style="border-border-color: #F2F2F2;line-"><blockquote><font color="#ff0000"><b>温馨提示</b></font>: 隐藏内容需要<a href="#respond" title="点击进行评论"> 回复评论 </a>后才能查看, 评论后请 <strong><a href="JavaScript:location.reload()" title="点击刷新"> 刷新 !</a></strong>.</blockquote></p>'), $atts));
$emAIl = null;
$user_ID = (int) wp_get_current_user()->ID;
if ($user_ID > 0) {
$email = get_userdata($user_ID)->user_email;
//对博主直接显示内容
$admin_email = "xxx@XXX.net"; //把左面的邮箱换成博主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');
?>

脚本中,邮箱需要换成我们当前WP程序中的管理员邮箱,这样对于管理员可见的。

第二、如何使用回复可见

[reply]回复可见的内容[/reply]

在我们需要隐藏的内容中用上面格式套用,这样就必须回答评论后才可以看到隐藏的内容。

返回顶部
顶部