Page 1 of 1
Protect Custom Post Types
Posted:
August 23rd, 2011, 12:09 am
by dev9833
Is there a way to protect all posts under specified custom post types? I'm using categories to protect the posts now which is working fine, but I'm worried down the road that my client could possibly uncheck the appropriate category or otherwise change the category hierarchy to cause a problem. Setting up the s2member restrictions based on custom post types would be so much easier.
Seems like something that would be a good core integration into the plugin anyway. If I'm missing something please let me know! Cheers.
Thanks in advance.
Re: Protect Custom Post Types
Posted:
August 23rd, 2011, 8:12 pm
by Jason Caldwell
Thanks for the excellent question
Hmm, I'll see what we can do about this in a future release.
That being said, s2Member's Post Level Access Protection is fully compatible with Custom Post Types. So if you want to, you can protect individual Posts this way, by ID, and it's OK if they're Custom Post Types.
The Category and/or Tag-based Restrictions are probably better though, so I think you're already on the right track. As far as the nesting goes, s2Member associates it's protection based on Category ID, and that does not change if/when the nesting order changes. So just make sure that you protect all any sub-categories inside the main category as well. That way if they're moved around and/or re-ordered, the client won't lose the protections applied by s2Member.
Again, I'll see what we can do to improve on this in a future release. We have some more work to do in this area already, related to bbPress Forum Topics, so perhaps we can look at this again at that time.
Re: Protect Custom Post Types
Posted:
August 23rd, 2011, 9:37 pm
by dev9833
Thanks for the reply Jason, I appreciate the level of service you guys consistently provide. Looking forward to future updates.
For others reading this post, I've implemented s2member protection on all instances of a specific custom post type by using URI restrictions in conjuction with CPT archive permalinks. I set up archives for custom post types, and then just used the slug of the CPT archive to protect any posts using that slug. So anything at example.com/custom-post-type-name/post-name would be protected because /custom-post-name/ is in the URI restrictions in s2membet options. Seems to work great and I don't have to worry about client deselecting a category.
Re: Protect Custom Post Types
Posted:
August 23rd, 2011, 11:50 pm
by Cristián Lávaque
Sounds like a great solution. Thanks for sharing it.
Another thing you may want to try is the customization Jason explains in the 2nd segment of the Custom Capabilities video, only instead of checking for a tag or category in the condition, it'd be a custom post type (if that's possible).
http://youtu.be/h7zBH938VbAI hope that helps!
Re: Protect Custom Post Types
Posted:
October 6th, 2011, 2:50 pm
by dhempy
Same needs here - Need to protect all Custom Post Type of type "Labs". No direct way to do this in s2member, and I don't trust my client to always tag or categorize new posts.
I tried using URI restrictions, but that had a problem: I DO want the whole world to see archive-labs.php, listing all the Labs with their teaser text, but you must be a member to actually open the Labs. I could probably work around the URL issue for the archive page, but the ultimately it would still not protect the labs when accessed other ways. (feeds, post ID, etc.)
I used the approach described in the video, but a little differently. My site uses a single-labs.php template. I added the following code to the top of that template:
- Code: Select all
<?php
if (! current_user_can("access_s2member_level2")) {
wp_redirect( home_url('/sign-up/') );
exit();
}
?>
This meets all my needs. I may change that to custom capabilities after I get some other things nailed down.
Having said that, missing Custom Post Types in s2member's restrictions seems like a big oversight to me. I hope you add that feature in the future.
Keep up the good work, guys!
-dave
Re: Protect Custom Post Types
Posted:
October 7th, 2011, 12:39 am
by Cristián Lávaque
I'm glad the tip Jason explains helped you work out a solution.
Thanks for the suggestion to include custom post types in the access restrictions!
Re: Protect Custom Post Types
Posted:
October 11th, 2011, 7:11 pm
by Jason Caldwell
Thanks for the heads-up on this thread.
OK, just to clarify for the benefit of other readers. s2Member DOES support Custom Post Types. You can protect Custom Post Types with s2Member's Post Level Access Restrictions in your Dashboard, under: s2Member -> Restriction Options -> Post Level Access Restrictions.
That being said, it is true there is no way to protect Custom Taxonomies ( i.e. Custom Categories that contain Custom Post Types ), other than with URI Restrictions; and we will try to improve upon this in a future release, definitely. @dhempy Just to confirm, this IS what you're referring to, correct?
Re: Protect Custom Post Types
Posted:
October 12th, 2011, 4:00 pm
by dhempy
I'm glad to hear that, Jason! I must have overlooked that. I'll give it a shot tomorrow.
No, I need to protect the entire CPT, without regard to taxonomies.
Thanks,
-dave
Re: Protect Custom Post Types
Posted:
October 14th, 2011, 1:51 pm
by dhempy
Ok, now I'm confused. I looked at "Post Level Access Restrictions ( optional )" and I see that I can protect individual posts, including posts that are of a Custom Post Type. However, I don't see the part where I can protect everything of a given CPT.
If I don't trust my client to tag or categorize each new "Lab" post to ensure it is protected, I certainly would not expect them to add each post ID to a list of protected posts on this page.
I hope I'm missing something and that you'll point me in the right direction. I really want to tell my users to simply create new Labs, and they will be protected without any more work from them. (And to be confident there's no way for them to accidentally screw it up!) I've pretty well reached that point by adding custom code to templates, but I'd readily abandon that if there was a turn-key solution to protecting an entire CPT.
Thanks,
-dave
Re: Protect Custom Post Types
Posted:
October 26th, 2011, 2:34 pm
by dhempy
Jason Caldwell wrote:OK, just to clarify for the benefit of other readers. s2Member DOES support Custom Post Types. You can protect Custom Post Types with s2Member's Post Level Access Restrictions in your Dashboard, under: s2Member -> Restriction Options -> Post Level Access Restrictions.
Jason - Can you expand on this? I don't see any way to protect a CPT on that page.
Do you mean that an individual post (e.g. id=124) can be protected, even if it is of a CPT? That's not what I'm looking for. I want *all* posts of a given CPT to be protected. Is there any way to do that?
Thanks, as always, for your great support, Jason!
-dave
Re: Protect Custom Post Types
Posted:
October 26th, 2011, 4:36 pm
by Jason Caldwell
Thanks for the follow-up.Right, Post Level Restrictions allow you to protect "all" Posts, or specific Post IDs, but s2Member does not have a built-in way to protect "all" Posts of a specific type yet.
You CAN protect specific Post IDs with s2Member using Post Level Restrictions, even if they are Custom Post Types
(that's fine to do that); but protecting "all" means that you're protecting "all" Posts
(of any kind), regardless of Post Type. So this is where you're having a problem.
In your case, here is what I would do...Create this directory and file:
/wp-content/mu-plugins/s2-hacks.php ( these are MUST USE plugins, that's what you want )Reference article:
http://codex.wordpress.org/Must_Use_Plugins- Code: Select all
<?php
add_action ("wp", "my_custom_s2_security_gate");
function my_custom_s2_security_gate ()
{
if (!is_admin() && is_singular ("my_custom_post_type") && !current_user_can ("access_s2member_level1"))
wp_redirect (get_page_link ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["membership_options_page"])) . exit ();
}
?>
Reference articles:
http://codex.wordpress.org/Conditional_ ... Attachmenthttp://devpress.com/blog/conditional-ch ... ost-types/
Re: Protect Custom Post Types
Posted:
October 27th, 2011, 1:44 pm
by dhempy
Looks good, Jason. I'll give that a try...
-dave
Re: Protect Custom Post Types
Posted:
November 1st, 2011, 7:39 pm
by Jason Caldwell
Protecting all Posts of a specific Post Type will become available in the next release.
( click to enlarge )
Re: Protect Custom Post Types
Posted:
November 2nd, 2011, 9:44 am
by dhempy
Wo0T!