Learn how to restrict viewing and adding comments to blog posts to subscribers only with Restrict Content Pro using code.
One of the benefits of being a member of the OsomCode is that you can suggest future tutorials.
For example, after reading the article where I explained how to restrict comments to subscribers in a CPT, a subscriber wondered how to do it only for “normal” post entries.
Interesting, isn’t it? Do you also want to restrict blog comments to subscribers only using Restrict Content Pro?
Here’s the code you need:
Instructions for hiding comments from non-subscribers on blog posts.
1. Create a snippet that limits comments
Add the following code snippet to 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 along with a function that checks if the user has an active subscription. In this case it’s rcp_user_has_active_membership (formerly rcp_is_active(), which is now deprecated).
So if the reader doesn’t meet the condition, this block takes care of deleting the comments using Genesis Framework hooks.
On the other hand, you limit the snippet to normal posts (blog articles) and it won’t affect the rest of your CPTs, where comments will still be open.
Also, with the above snippet you take the opportunity to place a message or CTA in the hidden area so that ‘non-subscribers’ understand why they don’t see the comments and you can invite them to subscribe or login.
Again, you can (and should) modify this text to your liking.
2. Customize the CSS of the CTA in the comment area
Once you’ve hidden the comments from non-subscribers, you just need to style 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 1097 code snippets of this website, login or subscribe here.
By inserting these CSS lines you ensure that the message is displayed as in this website (osomcode.com).
If you want the padlock to be displayed, add this file to the /assets/images/ path (hold down the alt key when clicking on the link to download it).
Your theme may not have the same folder structure and instead of /assets/images/ it may just be /images/ or similar. If so, put 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');
As always, I invite you to customize the size of the box, border, corporate colors…
Conclusions
Now you know how to hide comments to non-subscribers in normal blog posts and add a custom message inviting them to register or identify themselves.
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. 😉