Learn how to restrict viewing and adding comments in a Custom Post Type 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, one of our subscribers, following the article where I explained how to restrict comments to subscribers, left me this question:
Hi Nahuai,
One question about this snippet, how can I make it affect only a CPT I’ve created by code and not the comments of normal posts?
Best regards and thanks!
I thought that this was a very interesting option so I immediately got to work to on it.
Below you’re going to find out how to limit comment restriction to a CPT using Restrict Content Pro.
Instructions for hiding comments from non-subscribers in a CPT
1. Create a comment limiting snippet
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 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 a CPT, in this example, the portfolio. You only have to modify is_singular(‘portfolio’) by is_singular(‘yourcpt’) to adapt it to the CPT you are interested in.
In addition, with the previous fragment you can place a message or CTA in the hidden area so that ‘non-subscribers’ understand why they don’t see the comments and invite them to subscribe or identify themselves.
Again, you can (and should) modify this text to your liking.
IMPORTANT: if you have created the CPT using code, make sure you have added support for comments in the corresponding array.
For example:
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', 'comments', 'genesis-cpt-archives-settings', 'genesis-seo', 'genesis-scripts', 'genesis-layouts', 'genesis-rel-author', ),
Are you still not seeing the comments on your CTP?
Then check that they are enabled:
2. Customize the CSS of the CTA in the comment area
Once you’ve hidden the comments from non-subscribers, all you need to do is 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).
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 a specific CPT and add a custom message inviting them to register or identify themselves.
If you have any question, please leave it in the comments. And if you want to give me a suggestion for future snippets, please send it through the contact form.
Benefits of being a subscriber. 🙂