Only allow the post’s author to comment

Looking to only allow the post’s author to comment on a blog post? All you need is a create a filter in place to block others.

Filter to allow an author to comment

In order to allow only the post’s author to leave a comment on a blog post, add the following function into your theme’s functions.php file.

function only_post_author_to_comment( $is_open, $post_id ) {
     $is_open = false;
     $author_id = get_post_field( 'post_author', $post_id );
     if ( get_current_user_id() == $author_id || current_user_can( 'administrator' ) ) {
          $is_open = true;
     }
     return $is_open;
}

add_filter( 'comments_open', 'only_post_author_to_comment', 10, 2 );