我也在做同样的事情my website. 以下是我的做法。。。
Only post author and commentor can view each others comments
function restrict_comments( $comments , $post_id ){
global $post;
$user = wp_get_current_user();
if($post->post_author == $user->ID){
return $comments;
}
foreach($comments as $comment){
if( $comment->user_id == $user->ID || $post->post_author == $comment->user_id ){
if($post->post_author == $comment->user_id){
if($comment->comment_parent > 0){
$parent_comm = get_comment( $comment->comment_parent );
if( $parent_comm->user_id == $user->ID ){
$new_comments_array[] = $comment;
}
}else{
$new_comments_array[] = $comment;
}
}else{
$new_comments_array[] = $comment;
}
}
}
return $new_comments_array; }
add_filter( \'comments_array\' , \'restrict_comments\' , 10, 2 );
Only allow post author to reply to a comment on their post
add_action( \'pre_comment_on_post\', \'wpq_pre_commenting\' );
function wpq_pre_commenting( $pid ) {
$parent_id = filter_input( INPUT_POST, \'comment_parent\', FILTER_SANITIZE_NUMBER_INT );
$post = get_post( $pid );
$cuid = get_current_user_id();
if( ! is_null( $post ) && $post->post_author == $cuid && 0 == $parent_id ) {
wp_die( \'Sorry, you can only "Reply" to a message - click on the Reply link to send a message to the member who messaged you\' );
}
}
如果用户是当前帖子的作者,我还使用了一个函数来添加body类,这样我就可以设置评论表单的样式,这样帖子作者只能看到
reply 注释字段。。。
add_filter(
\'body_class\',
function( $classes ) {
if ( is_single() ) {
$post = get_queried_object();
$user = wp_get_current_user();
if ( $user->ID == $post->post_author ) {
$classes[] = \'post-author\';
}
}
return $classes;
}
);
css
.post-author #respond.comment-respond {display:none;}
.post-author .byuser #respond.comment-respond {display:inline;}