私密评论理解起来比较简单,就是别人看不到。这个东西如果严格实现起来还是有一些难度的,但是想要实现一个简易版还是可以的。
一般来讲WordPress 评论后都会保留评论者的cookies,我们根据这个cookie 来判断哪些评论是自己发出的,这样不至于自己都看不到自己的评论。站长理所当然可以看到所有的评论。
实际过程中还有一个问题,就是如果这条评论是回复给别人的而且被回复这个人并不是网站管理员,那么我们也是希望这个人也可以看到评论的。
如果浏览器的cookies 被清空,那么你将看不到你自己的私密评论。当你使用以前的邮箱重新评论后,你的私密评论将被标记为可见。
功能代码
function fa_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','fa_private_message_hook',10,2); function fa_mark_private_message( $comment_id ){ if ( $_POST['is-private'] ) { update_comment_meta($comment_id,'_private','true'); } } add_action('comment_post', 'fa_mark_private_message');
在评论的时候我们需要加一个是否发送私密评论的选项,这里要看你的主题评论表单是否使用了comment_form
,如果是自己拼接的html 则需要手动添加,使用了comment_form
直接添加如下功能代码即可。
add_filter( 'comment_form_defaults','fa_private_message_checkbox'); function fa_private_message_checkbox($default) { $default['comment_field'] .= '<p><input type="checkbox" name="is-private"></p>'; return $default; }
注意,输入评论内容的时候必须使用comment_text
或者get_comment_text
函数,私密评论前加上了#私密#
标记。
最后,还是那句话,如果你能用在你优秀的付费主题中,不妨也意思意思感谢下我。
好功能
但是评论发出后,如何编辑,转换私密和公开呢?(使用comment_form的情况下)