Learn how to restrict viewing and adding comments to subscribers only by using Restrict Content Pro in this tutorial.
One of the advantages of being an OsomCode subscriber is that you can propose tutorials. For example, a subscriber left me this suggestion:
How do you manage to restrict comments only to subscribers? Because with Restrict Content Pro I haven’t found this option. Is there any modification needed?
Yes, there is.
In OsomCode I have the comments accessible only to subscribers. Through them, logged-in users can ask questions about the code and other details about the tutorials.
Do you want to do the same for your project or your client’s? No problem.
Here’s how to enforce this restriction through code if you use Restrict Content Pro to manage your web subscriptions.
Instructions on how to hide comments to non-subscribers
1. Create a snippet that limits comments
Add the following code at the end of functions.php or in your functionality plugin:
To see this and another 1097 code snippets of this website, login or subscribe here.
When using Restrict Content Pro to manage subscribers, you need to add a conditional tag next to a feature that checks if the user has an active subscription. In this case it is rcp_user_has_active_membership (formerly rcp_is_active(), which is now deprecated).
Therefore, if the reader does not meet the condition, this code will remove the comments using the Genesis Framework hooks.
In addition, you can place a message or CTA in the hidden area so that the ‘non-subscribers’ understand why they do not see the comments and invite them to subscribe or identify themselves.
Of course, you can modify this text to your liking.
2. Customize the CTA CSS in the comment area
Once you’ve hidden the comments from non-subscribers, you just need to give the message a bit of style and it will appear in the restricted area.
To do this, add this snippet to the end of style.css:
To see this and another 1097 code snippets of this website, login or subscribe here.
By inserting these lines of CSS you ensure that the message is displayed as in this website.
If you want the lock to be shown, add this file to the /assets/images/ path (hold down the alt key when you click on the link to download it).
It is possible that your theme does not have the same folder structure and that instead of /assets/images/ it is just /images/ or similar. If so, place it in that folder and adjust the path accordingly.
You can also upload the image to your media library and place the full path in the ULR. It would look something like this:
background-image: url('https://osomcode.com/wp-content/uploads/2022/01/lock.svg');
Again, I invite you to customize the box size, border, corporate colors…
Conclusions
Now you know how I hide comments from non-subscribers and add a custom message inviting them to subscribe at OsomCode.
Any questions? Let me know 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. 😉