wordpress在实际运营的过程中往往需要用到很多功能,在能不安装插件的情况下本来还是不喜欢安装插件,可以通过PHP来实现回复可见的功能新增

添加教程

  1. 首先进入wordpress后台 ->> 外观 ->> 主题文件编辑器,选择当前启用主题下的functions.php文件

  2. 将如下代码添加进functions.php中即可实现回复可见

添加如下代码:

/* 添加回复可见功能 */
function reply_to_read($atts,$content=null){
extract(shortcode_atts(array("notice"=>'<span class="reply-to-read" style="font-size:16px; letter-spacing:3px; font-weight:bold; background-color:#f7f7f7; padding:10px;text-align: center;width:100%; color:#272727; border-radius:8px; border-style:dotted; border-width:2px; border-color:#959595; display:block;">此处内容需要<a style="color:#f5365c;" href="'. get_permalink().'#respond" title="评论本文">评论本文</a>后<a style="color:#f5365c;" 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
//对博主直接显示内容
$admin_email = "1240312603@qq.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]); //如果用户未登录但电脑上有本站的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');

短代码填写规范

[reply]你希望评论回复可见的内容[/reply]

实现效果如下

image-20220730002044963