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

本文档详细介绍了如何为Nana主题增加私密留言评论功能。通过编辑functions.php、comments.php和Nanaincfunctionswidgets.php三个文件,添加和修改特定代码,实现评论者和管理员可见的私密评论。用户在评论时勾选私密选项,即可创建仅对自己和网站管理员可见的评论。评论者在清除浏览器缓存后,将无法查看自己的私密评论。
摘要由CSDN通过智能技术生成

471a786e56a5bfe7f6e9a9ae42fe6864.png

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

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

//私密评论

function liao_private_message_hook( $comment_content , $comment){

$comment_ID = $comment->comment_ID;

$parent_ID = $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();

if ( $is_private ) $comment_content = ‘#私密# ‘ . $comment_content;

if ( $current_commenter[‘comment_author_email’] == $email || $parent_email == $current_commenter[‘comment_author_email’] || current_user_can(‘delete_user’) ) return $comment_content;

if ( $is_private ) return ‘该评论为私密评论’;

return $comment_content;

}

add_filter(‘get_comment_text’,’liao_private_message_hook’,10,2);

function liao_mark_private_message( $comment_id ){

if ( $_POST[‘is-private‘] ) {

add_comment_meta($comment_id,’_private’,’true’);

}

}

add_action(‘comment_post’, ‘liao_mark_private_message’);

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

<?php  comment_id_fields(); do_action(‘comment_form’, $post->ID); ?>

修改为:

私密评论

<?php  comment_id_fields(); do_action(‘comment_form’, $post->ID); ?>

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

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

修改为

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

即可。

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

2e615b1d1fe222d65d044d0462a13041.png

记得勾选私密评论按钮

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

ca381affcd4619f636b2d8463beb88d2.png

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

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

e4be96a4ef0e822ff3bd616af2b2fc1a.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值