Membership site owners often need to control who can view and engage with content. While some posts remain public, comments may contain valuable discussions meant only for members.
This guide shows you how to restrict comment visibility on public posts to logged-in members, ensuring exclusive engagement. You can also prevent spam and unwanted interactions by stopping non-members from leaving comments on your public posts.

Understanding the Code Recipe
This code checks whether a visitor is logged in and has an active membership before displaying comments on public posts. This ensures that only members can engage in discussions, preventing spam and maintaining a community of verified users.
The function comments_template( $file, true )
controls whether the comment section appears. If the user is not logged in or lacks a membership, comments remain hidden, reinforcing exclusivity and encouraging sign-ups for access.
About the Code Recipe
This code hides comments on public posts unless the user is logged in with an active membership. It ensures discussions remain within your member community while keeping content publicly visible.

When the “Users must be registered and logged in to comment” box is unchecked under Settings > Discussion on your WordPress dashboard, visitors will still see the “Leave a Reply” comment form. They would not be able to view or read comments.
To prevent members of the public from “leaving a comment”, check the “Users must be registered and logged in to comment” box.

The Code Recipe (PHP)
Adding the Recipe to Your Website
You can add this recipe to your site by creating a custom plugin or using the Code Snippets plugin available for free in the WordPress repository. Read this companion article for step-by-step directions on either method.
Customizations
Specific Membership Levels
Modify line 22 to only allow specific membership levels to view comments. For example, to allow only members with levels 2 and 3, use the code below:
if ( function_exists( 'pmpro_hasMembershipLevel' ) && ! pmpro_hasMembershipLevel( array( 2, 3 ) ) ) {
Display Message
Modify the message displayed by replacing lines 40 to 44 with the following snippet to display a custom message instead of the default one:
function pmpro_non_member_text_before_comment_form() {
echo wp_kses_post( __( 'Only members can participate in the discussion. Join now to comment.', 'pmpro-comments' ) );
}
Replace Only members can participate in the discussion. Join now to comment
with your custom message.
You can also modify the message displayed to non members in the pmpro_non_member_text_before_comment_form()
function by updating the echo statement on line 38 to show a custom message instead of the default one.