WordPress站点给文章增加私密留言评论功能的教程

wp教程评论27.2K views阅读模式

WordPress 站点的评论默认情况下都是公开的评论,也就是说只要是审核通过的评论,所有人(包括站长、留言者、游客等)都可以看到这条评论内容。如果我们想要实现只有留言者和站长看到的私密留言评论,那么应该怎么实现呢?

Nana 主题增加私密留言评论的功能步骤

1、打开 Nana主题的 functions.php 文件,在最后一个 ?> 的前面添加以下代码:

  1. //私密评论
  2. function liao_private_message_hook( $comment_content , $comment){
  3.     $comment_ID = $comment->comment_ID;
  4.     $parent_ID = $comment->comment_parent;
  5.     $parent_email = get_comment_author_email($parent_ID);
  6.     $is_private = get_comment_meta($comment_ID,'_private',true);
  7.     $email = $comment->comment_author_email;
  8.     $current_commenter = wp_get_current_commenter();
  9.     if ( $is_private ) $comment_content = '#私密# ' . $comment_content;
  10.     if ( $current_commenter['comment_author_email'] == $email || $parent_email == $current_commenter['comment_author_email'] || current_user_can('delete_user') ) return $comment_content;
  11.     if ( $is_private ) return '<span style="color:#A6A6A6"><i class="fa fa-lock fa-fw"></i>该评论为私密评论</span>';
  12.     return $comment_content;
  13. }
  14. add_filter('get_comment_text','liao_private_message_hook',10,2);
  15. function liao_mark_private_message( $comment_id ){
  16.     if ( $_POST['is-private'] ) {
  17.         add_comment_meta($comment_id,'_private','true');
  18.     }
  19. }
  20. add_action('comment_post', 'liao_mark_private_message');

 

2、打开 Nana主题的 comments.php 文件,找到以下代码:

  1. <p class="form-submit">
  2.     <input id="submit" name="submit" type="submit" tabindex="5" value="提交评论">
  3.     <?php comment_id_fields(); do_action('comment_form', $post->ID); ?>
  4. </p>

修改为:

  1. <p class="form-submit">
  2.     <input id="submit" name="submit" type="submit" tabindex="5" value="提交评论">
  3.     <span style="margin-right: 10px;"><input type="checkbox" name="is-private">私密评论</span>
  4.     <?php comment_id_fields(); do_action('comment_form', $post->ID); ?>
  5. </p>

3、打开 Nana主题的Nana\inc\functions\widgets.php 文件,找到以下代码:

  1. <?php echo convert_smilies($my_comment->comment_content); ?>

修改为

  1. <?php echo liao_private_message_hook($my_comment->comment_content,$my_comment);?>

即可。

至此,主题已经具备了私密留言评论的功能。只需要我们在评论时,勾选私密评论即可。具体如下图所示:

WordPress站点给文章增加私密留言评论功能的教程

记得勾选私密评论按钮

提交评论后,评论者本人是可以在当前页面和侧边栏近期评论中看到具体的评论内容,但是如果清空浏览器缓存的话,连评论者自己也是无法看到评论内容的。具体如下图所示:

WordPress站点给文章增加私密留言评论功能的教程

只有评论者本人和站长可见

如果不是评论者或站长的话,其他游客用户是无法看到具体的评论内容的,只能看到“该评论为私密评论”的字样。具体如下图所示:

WordPress站点给文章增加私密留言评论功能的教程

企鹅SEO
  • 本文由 发表于 2022年10月10日 11:57:01
  • 转载请务必保留本文链接:https://www.qieseo.com/4653.html

发表评论