• Skip to primary navigation
  • Skip to main content
OsomCode

OsomCode

Awesome code tutorials for WordPress and Genesis

  • Subscribe
  • My account
    • Edit profile
    • Favorites
    • Update credit card
  • Login

Display comments only to active Restrict Content Pro subscribers in a block theme

Learn in this tutorial how to restrict viewing and adding comments only for active Restrict Content Pro subscribers in a block theme.

In previous tutorials you learned how to restrict comments only to active RCP subscribers in Genesis child themes.

But block themes are already a reality and they are going to be used more and more, so in this tutorial you are going to see how you can achieve the same but using a new generation theme.

message displayed comments users not subscribed RCP
Message displayed to an unsubscribed user (Twenty Twenty-Three)

Here is the code you need:

Instructions for hiding comments from users who do not have an active RCP subscription

1. Create a snippet that limits comments

Add the following code snippet to your functionality plugin:

To see this and another 883 code snippets of this website, login or subscribe here.

In this code you use the rcp_user_has_active_membership function to check if the user has an active subscription and the render_block filter to display a message in case the reader does not meet the condition.

In addition, you take advantage of the message or CTA in the hidden area so that non-subscribers understand why they do not see the comments and invite them to subscribe or identify themselves.

You can modify both the text displayed and the URL where to register (changing /register/ for the slug where your registration page is).

The above code would apply to all posts with comments, but if you are interested in limiting it to normal posts (blog articles) or some Custom Post Type in particular, you would have to add a conditional tag to make it so. For example:

To see this and another 883 code snippets of this website, login or subscribe here.

In this case, by using is_singular('course') you get the message to be displayed only in the course CPT entries. Just replace it with the CPT you are interested in.

Finally, if you are using a plugin that displays a modal window to speed up user login, you must also modify the above code. This would be the code to integrate it with Osom Modal Login:

To see this and another 883 code snippets of this website, login or subscribe here.

In this case, clicking on the text “login” will open a modal window to enter the user name and password.

2. Customize the CTA CSS in the comments area

Once you have hidden the comments from non-subscribers, you just need to give a little style to the message that appears in the restricted area.

To do this, add this block to the end of style.css:

To see this and another 883 code snippets of this website, login or subscribe here.

By inserting these CSS lines you ensure that the message is displayed respecting the Twenty Twenty-Three theme styles.

message displayed comments users not subscribed RCP
Message displayed to a non-subscribed user (Twenty Twenty-Three)

In this case, for the links you use the secondary color since the primary is a very light green that doesn’t have enough contrast with the white background. But if in the theme you are using the combination has good contrast you could use the primary color. This would be the code:

.rcp-restricted a {
    color: var(--wp--preset--color--primary);
}

You can also add colors using hex code or font size with pixels and/or rem, but I recommend you get used to using CSS variables.

I also invite you to finish customizing the design to your liking. 😉

Conclusions

Now you know how to hide comments to non-subscribers on one or several post types in your block theme, and add a custom message inviting them to register or login.

Any questions? I read you in the comments.

And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉

Comments CSS FSE PHP Post Restrict Content Pro WordPress

To leave read and make questions about this code, you can login or register.

  • About OsomCode
  • FAQ
  • Contact

Legal Notice · Privacy Policy · Cookie Policy · Terms and conditions
Copyright © 2023

Lost your password?