i want restrict my registered authors to write comment to other posts except his own posts. Is it possible? I have no idea how to do it. May be need conditional logic but how?
-
I assume you only want them to answer comments on their own posts? Or do you want to keep them from leaving any comments on any other post?– WSUJun 5, 2019 at 9:34
-
both of them.. is it possible– Ramin MahmudovJun 5, 2019 at 10:30
-
The first one is definitely possible. I work with a site where it is like that. I'll try to find out how it works. I'm not sure about the second part. I have never heard of that before.– WSUJun 5, 2019 at 12:47
-
thank yo so much. i am looking forward for your reply– Ramin MahmudovJun 5, 2019 at 13:08
-
So, I just tested this on my local site and if you give someone the user role "author" they should be able to only reply to and manage comments that are left below their own posts. Below that, you can't reply to comments at all, above author capabilities you are able to manage all comments. So, if you set your users to authors, you can at least limit them to manage comments only for their own content. Hope this helps!– WSUJun 6, 2019 at 13:12
1 Answer
I have the same thing running on my website. Here is how I do do it...
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' );
}
}
I also used a function to add a body class if the user was the current post's author so I could style the comment form so that the post author could only see the reply comment field...
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;}
-
1
-
1You're welcome. This effectively turns the WP commenting system into a private messaging system.– PeteAug 26, 2019 at 9:26
-
Thanks so much for this code, but getting an error - any advice would be greatly appreciated. Warning: count(): Parameter must be an array or an object that implements Countable in Feb 21, 2022 at 20:11