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.

Featured Image for code recipe post on restricting comments on public posts to members only

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.

Screenshot that says you must be a member to access this content

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.

Screenshot of the Discussion Settings in the WP dashboard

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.

Free Course: Membership Site Development—The Basics

Develop a deeper understanding of membership site development in this beginner-level course. Learn how to make your site work better, save yourself time and money, and improve your site's performance.

Featured Image for Membership Site Development Course: The Basics
Was this article helpful?
YesNo