子比主题 – 新版私密评论功能
这是一款子比主题新私密评论功能,这款是适配了子比的样式,在此之前腾飞博客给大家分享过改文件的代码,但是那些太麻烦了,今天腾飞博客给大家分享一款无需修改子比文件即可实现的,喜欢的自行部署吧!2025/9/14修复
[*]修复当从私密设为不私密之后,未登录用户看不到评论
[*]添加版本二提交评论按钮样式(与旧版本一样的按钮)
代码部署
首先我写了两个版本,第一个是点该评论下面三个点有私密,第二个就是评论下面三个点也有而且再加上提交评论旁边也有一个私密按钮,这样用户很方便去加私密,推荐版本二的效果(本站正在使用的),不过要修改子比文件,很简单的!
定位:/wp-content/themes/zibll/func.php文件,没有这个文件自己创建一个,记得加上php头,要不然会报错,将下面的代码直接丢里面即可!
// 腾飞博客修复私密功能
function zib_private_message_hook($comment_content, $comment) {
$comment_ID = $comment->comment_ID;
$is_private = get_comment_meta($comment_ID, '_private', true);
$current_user = wp_get_current_user();
$html = '<div class="hidden-box" reply-show="true" reload-hash="#hidden-box-comment"><a class="hidden-text"><i class="fa fa-exclamation-circle mr10"></i>此评论为私密评论,仅双方可见.</a></div>';
if ($is_private) {
$is_comment_author = false;
$is_post_author = false;
$is_admin = current_user_can('manage_options');
if ($current_user && $current_user->ID) {
$is_comment_author = ($current_user->ID == $comment->user_id);
$is_post_author = ($current_user->ID == get_post_field('post_author', $comment->comment_post_ID));
}
if ($is_admin || $is_comment_author || $is_post_author) {
return '<div class="hidden-box show" id="hidden-box-comment"><div class="hidden-text">私密评论内容</div>' . $comment_content . '</div>';
}
return $html;
}
return $comment_content;
}
add_filter('get_comment_text', 'zib_private_message_hook', 10, 2);
function zib_comments_action_add_private($lists, $comment) {
$current_user = wp_get_current_user();
$user_id = get_current_user_id();
if (is_user_logged_in() && (is_super_admin($user_id) || $current_user->ID == $comment->user_id)) {
$comment_ID = $comment->comment_ID;
$is_private = get_comment_meta($comment_ID, '_private', true);
$private_text = empty($is_private) ? '设为私密' : '取消私密';
$action = empty($is_private) ? 'set_private' : 'del_private';
$private_but = '<a class="comment-private-link wp-ajax-submit" form-action="' . esc_attr($action) . '" form-data="' . esc_attr(json_encode(['id' => $comment_ID])) . '" href="javascript:;"><i class="fa fa-user-secret mr10" aria-hidden="true"></i>' . $private_text . '</a>';
$lists = '<li>' . $private_but . '</li>' . $lists;
}
return $lists;
}
add_filter('comments_action_lists', 'zib_comments_action_add_private', 99, 2);
function zib_private_comment_action() {
$response = ['reload' => true];
if (!isset($_POST['action']) || !$_POST['action']) {
zib_send_json_error(['msg' => '无效的操作类型'] + $response);
}
if (!isset($_POST['id']) || !$_POST['id']) {
zib_send_json_error(['msg' => '无效的评论ID'] + $response);
}
$comment_id = intval($_POST['id']);
$action = sanitize_text_field($_POST['action']);
$comment = get_comment($comment_id);
$post_author_id = get_post_field('post_author', $comment->comment_post_ID);
$current_user = wp_get_current_user();
if (!$comment_id || !$current_user || !($current_user->ID == $comment->user_id || current_user_can('manage_options'))) {
zib_send_json_error(['msg' => '权限不足或无效的评论ID'] + $response);
}
if ($action === 'set_private') {
update_comment_meta($comment_id, '_private', 'true');
zib_send_json_success(['msg' => '评论已设为私密'] + $response);
} elseif ($action === 'del_private') {
delete_comment_meta($comment_id, '_private');
zib_send_json_success(['msg' => '评论已公开'] + $response);
} else {
zib_send_json_error(['msg' => '无效的操作类型'] + $response);
}
}
add_action('wp_ajax_set_private', 'zib_private_comment_action');
add_action('wp_ajax_del_private', 'zib_private_comment_action');
定位:/wp-content/themes/zibll/func.php文件,没有这个文件自己创建一个,记得加上php头,要不然会报错,将下面的代码直接丢里面即可!
// 腾飞博客修复私密功能 - 版本二
function zib_private_message_hook($comment_content, $comment) {
$comment_ID = $comment->comment_ID;
$is_private = get_comment_meta($comment_ID, '_private', true);
$current_user = wp_get_current_user();
$html = '<div class="hidden-box" reply-show="true" reload-hash="#hidden-box-comment"><a class="hidden-text"><i class="fa fa-exclamation-circle mr10"></i>此评论为私密评论,仅双方可见.</a></div>';
if ($is_private) {
$is_comment_author = false;
$is_post_author = false;
$is_admin = current_user_can('manage_options');
if ($current_user && $current_user->ID) {
$is_comment_author = ($current_user->ID == $comment->user_id);
$is_post_author = ($current_user->ID == get_post_field('post_author', $comment->comment_post_ID));
}
if ($is_admin || $is_comment_author || $is_post_author) {
return '<div class="hidden-box show" id="hidden-box-comment"><div class="hidden-text">私密评论内容</div>' . $comment_content . '</div>';
}
return $html;
}
return $comment_content;
}
add_filter('get_comment_text', 'zib_private_message_hook', 10, 2);
function zib_comments_action_add_private($lists, $comment) {
$current_user = wp_get_current_user();
$user_id = get_current_user_id();
if (is_user_logged_in() && (is_super_admin($user_id) || $current_user->ID == $comment->user_id)) {
$comment_ID = $comment->comment_ID;
$is_private = get_comment_meta($comment_ID, '_private', true);
$private_text = empty($is_private) ? '设为私密' : '取消私密';
$action = empty($is_private) ? 'set_private' : 'del_private';
$private_but = '<a class="comment-private-link wp-ajax-submit" form-action="' . esc_attr($action) . '" form-data="' . esc_attr(json_encode(['id' => $comment_ID])) . '" href="javascript:;"><i class="fa fa-user-secret mr10" aria-hidden="true"></i>' . $private_text . '</a>';
$lists = '<li>' . $private_but . '</li>' . $lists;
}
return $lists;
}
add_filter('comments_action_lists', 'zib_comments_action_add_private', 99, 2);
function zib_private_comment_action() {
$response = ['reload' => true];
if (!isset($_POST['action']) || !$_POST['action']) {
zib_send_json_error(['msg' => '无效的操作类型'] + $response);
}
if (!isset($_POST['id']) || !$_POST['id']) {
zib_send_json_error(['msg' => '无效的评论ID'] + $response);
}
$comment_id = intval($_POST['id']);
$action = sanitize_text_field($_POST['action']);
$comment = get_comment($comment_id);
$post_author_id = get_post_field('post_author', $comment->comment_post_ID);
$current_user = wp_get_current_user();
if (!$comment_id || !$current_user || !($current_user->ID == $comment->user_id || current_user_can('manage_options'))) {
zib_send_json_error(['msg' => '权限不足或无效的评论ID'] + $response);
}
if ($action === 'set_private') {
update_comment_meta($comment_id, '_private', 'true');
zib_send_json_success(['msg' => '评论已设为私密'] + $response);
} elseif ($action === 'del_private') {
delete_comment_meta($comment_id, '_private');
zib_send_json_success(['msg' => '评论已公开'] + $response);
} else {
zib_send_json_error(['msg' => '无效的操作类型'] + $response);
}
}
add_action('wp_ajax_set_private', 'zib_private_comment_action');
add_action('wp_ajax_del_private', 'zib_private_comment_action');
add_action('comment_post', function($comment_id) {
if (isset($_POST['is-private'])) {
update_comment_meta($comment_id, '_private', 'true');
}
});将下面的代码放到:/wp-content/themes/zibll/template/comments.php,不会放的看下图,我们进到文件可以搜“提交评论”,如下图!
<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>
页:
[1]