这是一款子比主题私密评论的功能,这个功能带有设置评论的私密和删除私密的功能,非常实用的一个功能,我觉得很不错的一个加密评论,喜欢的自行部署吧!
![图片[1]-子比主题 – 评论区加一个私密评论功能-雾祈心语](https://bkm.mistora.cc/wp-content/uploads/2026/01/image-24-1024x468.png)
![图片[2]-子比主题 – 评论区加一个私密评论功能-雾祈心语](https://bkm.mistora.cc/wp-content/uploads/2026/01/image-25-1024x471.png)
首先我们要在目录:/wp-content/themes/zibll/func.php文件,没有这个文件自己创一个,记住创建之后要记得加php头,话不多说直接开始,将下面的代码放到:/wp-content/themes/zibll/func.php文件里面
function zyx_scripts(){
if (!is_admin()) {
$script = array(
'smminjs' => '改为你的js链接',
);
foreach( $script as $k => $v ){
wp_register_script( $k, $v, array(), '2.4.0', true);
};
wp_enqueue_script('smminjs');
if (is_singular()) {
wp_enqueue_script('smminjs');
};
}
}
add_action('wp_enqueue_scripts', 'zyx_scripts');
function zyx_private_message_hook( $comment_content , $comment){
$comment_ID = $comment->comment_ID;
$parent_ID = $comment->comment_parent ? $comment->comment_parent : '';
$parent_email = get_comment_author_email($parent_ID);
$is_private = get_comment_meta($comment_ID,'_private',true);
$email = $comment->comment_author_email;
$current_commenter = wp_get_current_commenter();
$current_user = wp_get_current_user();
$html = '<span style="color:#558E53"><i class="fa fa-lock fa-fw"></i>该评论为私密评论</span>';
if ( $is_private ) {
if ( !is_user_logged_in() && $current_commenter['comment_author_email'] == '' ) {
return $comment_content = $html;
}else
if ($current_commenter['comment_author_email'] == '' && $current_user->user_email == $parent_email || current_user_can('delete_user') || $current_user->user_email == $email || $current_commenter['comment_author_email'] == $email || $parent_email == $current_commenter['comment_author_email'] && $current_commenter['comment_author_email'] !== ''){
return $comment_content = '#私密# ' . $comment_content;
}
return $comment_content = $html;
}
return $comment_content;
}
add_filter('get_comment_text','zyx_private_message_hook',10,2);
function zyx_mark_private_message( $comment_id ){
if ( $_POST['is-private'] ) {
update_comment_meta($comment_id,'_private','true');
}
}
add_action('comment_post', 'zyx_mark_private_message');
//将某条评论设为私密评论
add_action('wp_ajax_nopriv_mrhe_private', 'zyx_private');
add_action('wp_ajax_mrhe_private', 'zyx_private');
function zyx_private(){
$comment_id = $_POST["p_id"];
$action = $_POST["p_action"];
if ( $action == 'set_private'){
update_comment_meta($comment_id, '_private', 'true');
}
if ($action == 'del_private'){
delete_comment_meta($comment_id, '_private','true');
}
echo 'ok';
die;
}
//挂载到评论底部
function zyx_footer_info_add_private($info, $comment) {
if ( current_user_can( 'manage_options' ) ) {
$comment_ID = $comment->comment_ID;
$i_private = get_comment_meta($comment_ID, '_private', true);
$flag = ''; // 初始化 $flag 为空字符串
if (empty($i_private)) {
$flag .= ' - <a href="javascript:;" rel="external nofollow" rel="external nofollow" data-actionp="set_private" data-idp="' . get_comment_id() . '" id="sp" class="sm">(<span class="has_set_private">设为私密</span>)</a>';
$info = $info . $flag;
} else {
$flag .= ' - <a href="javascript:;" rel="external nofollow" rel="external nofollow" data-actionp="del_private" data-idp="' . get_comment_id() . '" id="sp" class="sm">(<span class="has_set_private">删除私密</span>)</a>';
$info = $info . $flag;
}
}
return $info;
}
add_filter('comment_footer_info', 'zyx_footer_info_add_private', 99, 2);
然后我们将下面的代码放到:/wp-content/themes/zibll/template/comments.php,不会放的看下图
![图片[3]-子比主题 – 评论区加一个私密评论功能-雾祈心语](https://bkm.mistora.cc/wp-content/uploads/2026/01/image-26-1024x511.png)
<label class="but c-blue pw-1em" data-placement="top" data-toggle="tooltip" title="你的评论仅评论双方可见。" style="margin-bottom: 0px;">
<input name="is-private" type="checkbox">私密</label>
可以看func文件那个地方,那个里面的代码是不是有一个'smminjs' => '改为你的js链接',那么这个就是js代码的链接,怎么弄链接就不用说了吧,自己创建文件放进去然后拿到目录链接放里面!
$(function() {
$(".links-card ul.list-inline img").each(function() {
if ("" == $(this).attr("data-src")) {
var a = "" + $(this).parent().attr("href");
$(this).attr("src", a)
}
}), $(document).on("click", ".sm", function() {
var a = $(this);
if (a.hasClass("private_now")) return notyf("您之前已设过私密评论", "warning"), !1;
a.addClass("private_now");
var t = a.data("idp"),
e = a.data("actionp"),
n = a.children(".has_set_private"),
r = {
action: "mrhe_private",
p_id: t,
p_action: e
};
return $.post("/wp-admin/admin-ajax.php", r, function(a) {
n.html(a)
}), !1
})
});
© 版权声明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
THE END

















暂无评论内容