latest stable versions: v150827 (changelog)

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Custom Capabilities Assistance Please

Home Forums Community Forum Custom Capabilities Assistance Please

This topic contains 6 replies, has 2 voices. Last updated by  Alan Ashbaugh 3 years, 11 months ago.

Topic Author Topic
Posted: Friday Jan 11th, 2013 at 10:43 am #37010

Hi,

I’ve been trying to implement custom capabilities packages but am having difficulty. I’ve watched the 1st video and 2nd video (5 and 15 minutes) on doing so, and I’m using the below code:

add_action ("template_redirect", "my_custom_capabilities");

function my_custom_capabilities ()
	{
	if(has_tag("smartbuying") && !current_user_can ("access_s2member_ccap_level2") && !current_user_can ("access_s2member_ccap_smartbuying"))
		{
		header ("Location: ".S2MEMBER_MEMBERSHIP_OPTIONS_PAGE_URL);
		exit();
		}
	}

The goals are these:

1. Have a restricted set of pages, which I’ve already set in the S2Members settings, which are only accessible with Level 2 or higher. Level 2 is the all access level. (I know this probably should be level 1, but let’s leave it if that’s cool)

2. Be able to assign custom capabilities to users who are not Level 2, so they can access bits and pieces. Level 2 would be all access.

For example, I’ve added certain pages (and added ability to tag pages), and tagged them with “smartbuying”. I’d like to be able to add the “smartbuying” custom capability to users who are Level 3, or even subscriber Level 0, so that they will be restricted from all pages EXCEPT the custom capabilities that they have, such as smartbuying.

Also, I’m curious if in the above code, does redirecting to S2MEMBER_MEMBERSHIP_OPTIONS_PAGE_URL mean that the user will be blocked, or meaning that they will be let through?

I’m an Intermediate WPer and beginning PHPer, so I’d love your assistance in creating these custom packages based on tags. The end goal is to allow my client to add tags based on the packages he wants, and then assign those packages (custom capabilities) to users regardless of Level. Then, to have the Level 2+ be all access.

Thanks!

List Of Topic Replies

Viewing 6 replies - 1 through 6 (of 6 total)
Author Replies
Author Replies
Posted: Saturday Jan 12th, 2013 at 10:46 am #37131

Oh, I guess you’re away for the weekend. Well, I hope to hear from you soon. Thanks!

Posted: Sunday Jan 13th, 2013 at 1:02 pm #37272
Staff Member

Thanks for your inquiry. ~ We appreciate your patience :-)

Note. In this line of the code, please update this to use the “wp” action hook instead. The video is a little outdated now, and this hook is a more reliable option with newer WordPress installations. The “template_redirect” comes later, and “wp” is a better one to use please.
add_action ("wp", "my_custom_capabilities");

Also, I’m curious if in the above code, does redirecting to S2MEMBER_MEMBERSHIP_OPTIONS_PAGE_URL mean that the user will be blocked, or meaning that they will be let through?

You’re blocking access, and instead redirecting them to your Membership Options Page, so they can pay you for access. Feel free to change that to whatever you prefer. If you have a special page that will display information more specific to this scenario, please use that instead. Just hard-code a URL of your choosing if you like.

1. Have a restricted set of pages, which I’ve already set in the S2Members settings, which are only accessible with Level 2 or higher. Level 2 is the all access level. (I know this probably should be level 1, but let’s leave it if that’s cool)

2. Be able to assign custom capabilities to users who are not Level 2, so they can access bits and pieces. Level 2 would be all access.

I see your dilemma. These two concepts really conflict with each other, because content protected at Level #2 or higher, is based on Membership “Levels”, whereas Capabilities provide more control, and grant access to specific things. So in this scenario, the fact that you have content blocked (and it requires Level #2), really goes against what you’re trying to accomplish in my opinion.

I suggest that you unblock the content from Level #2, and instead require Level #1 only. Then go back to your Custom Capabilities and do what you need to do with those. This way anyone that IS a Member (i.e. they have a Level #1 Membership), can gain access of some kind, based on your Custom Capabilties.

Now, in your Custom Capabilties, you might have two of them.

  • Custom Capability “all”, which provides access to anything/everything, so long as they ARE a paying Member at Level #1 or higher.
  • Custom Capability “limited”, which provides access to a subset of information on your site.
  • You could of course, create additional Custom Capabilities that might break down what “limited” really means (i.e. music, videos, or something more specific if you like).

So you might end up with a custom script like this:

add_action ("wp", "my_custom_capabilities");

function my_custom_capabilities ()
	{
	if(has_tag("smartbuying") && !current_user_can ("access_s2member_ccap_all") && !current_user_can ("access_s2member_ccap_limited"))
		{
		header ("Location: ".S2MEMBER_MEMBERSHIP_OPTIONS_PAGE_URL);
		exit();
		}
	}
Posted: Wednesday Jan 16th, 2013 at 3:26 pm #37820

Hi,

Thanks for your reply. I just have a couple more follow up questions to make sure I implement this correctly.

So, assuming we’re using your code example (in previous post), I would need to:

A. In the Page Restrictions area, in Level 1, enter all the IDs of all the restricted pages on the site.
B. Change all paying members of the site from Level 2 to Level 1.
Question: Would this not work if I just left the page restrictions on Level 2 and made all paying members Level 2?
C. On a per page basis, give every single restricted page on the site the custom capability of “all”
D. On a per page basis, give certain pages other custom capabilities, such as “limited”.
E. Add the custom code to functions.php to support these custom groupings.

Questions:
+ Is there anything else I need to do to make this work?
+ If I do it this way it seems that using tags won’t do much, as it will all be based on the custom capabilities. I was hoping I could create the custom capabilities groupings using tags alone, as opposed to adding custom capabilities labels to every single page.
+ Is there any way to bulk assign custom capabilities, as we have many pages on our site?

Thanks!
Alan

Posted: Thursday Jan 17th, 2013 at 5:07 pm #37944
Staff Member

Thanks for your reply :-)

A. In the Page Restrictions area, in Level 1, enter all the IDs of all the restricted pages on the site.

That is correct.

B. Change all paying members of the site from Level 2 to Level 1.
Question: Would this not work if I just left the page restrictions on Level 2 and made all paying members Level 2?

Yes. Either way would be fine.

C. On a per page basis, give every single restricted page on the site the custom capability of “all”

Correct.

D. On a per page basis, give certain pages other custom capabilities, such as “limited”.

Correct.

E. Add the custom code to functions.php to support these custom groupings.

Correct.

Questions:
+ Is there anything else I need to do to make this work?

No. That would be it.


+ If I do it this way it seems that using tags won’t do much, as it will all be based on the custom capabilities. I was hoping I could create the custom capabilities groupings using tags alone, as opposed to adding custom capabilities labels to every single page.
+ Is there any way to bulk assign custom capabilities, as we have many pages on our site?

I’m VERY glad you’re asking this question, because with everything listed above, that’s a LOT of manual work to implement and maintain. Instead of trying to implement all of this within the UI, all you really need to do is sell Custom Capabilities and assign Tags to your Posts as they’re being published. That’s easy enough.

So in the following example, we might have two Tags that we use.

1. If I publish a new Post that requires Membership, I would give it the Tag members-only.

2. If I publish a new Post that requires Membership, but it ALSO requires some additional paid access (e.g. an additional Capability), I would give it both of these Tags.
members-only, additional-access-required.

Of course, you can name your Tags whatever you prefer.

Now… You might have something like this, which would encompass everything you’re trying to accomplish here, in a more custom way, for your business model.

Please create this directory and file:
/wp-content/mu-plugins/s2-hacks.php
(NOTE: these are MUST USE plugins, see: http://codex.wordpress.org/Must_Use_Plugins)
(See also: http://www.s2member.com/kb/hacking-s2member/)

<?php
add_action ('wp', 'my_custom_capability_handler');

function my_custom_capability_handler()
	{
		if(is_singular() && has_tag('members-only')) // Restricted?
			{
				if(!current_user_can('access_s2member_ccap_members_only'))
				// Is this a Member that I sold the Custom Capability: `members_only` to?
				// If NOT, we redirect them to the Membership Options Page so they can purchase.
					{
						header('Location: '.S2MEMBER_MEMBERSHIP_OPTIONS_PAGE_URL);
						exit;
					}
				
				if(has_tag('additional-access-required')) // Also requires additional access?
					{
						if(!current_user_can('access_s2member_ccap_additional_access'))
						// Is this a Member that I also sold the Custom Capability: `additional_access` to?
						// If NOT, we redirect them to the Membership Options Page so they can purchase.
							{
								header('Location: '.S2MEMBER_MEMBERSHIP_OPTIONS_PAGE_URL);
								exit;
							}
					}
			}
	}
&#91;/hilite&#93;
&#91;info_box&#93;
If you were to use something like this, all you do is sell Custom Capabilities. You're protecting your content by it's association with the Tags I listed above. Nice! The &#91;hilite path&#93;Dashboard -› s2Member® -› Restriction Options&#91;/hilite&#93; panel would not be used in a case like this, because you're implementing something more custom here.
&#91;/info_box&#93;

<h4>Recommended Reading/Viewing...</h4>
See: Dashboard -› s2Member® -› API / Scripting -› Advanced PHP Conditionals

Video » s2Member (Custom Capabilities)

Posted: Thursday Jan 17th, 2013 at 5:15 pm #37948
Staff Member

If you need to give MANY Users/Members a Custom Capability, here is an example script to accomplish it for you.

Please create this directory and file:
/wp-content/mu-plugins/s2-hacks.php
(NOTE: these are MUST USE plugins, see: http://codex.wordpress.org/Must_Use_Plugins)
(See also: http://www.s2member.com/kb/hacking-s2member/)

<?php
add_action('init', 's2_update_customer_ccaps');
function s2_udpate_customer_ccaps()
	{
		if(!empty($_GET&#91;'s2_update_customer_ccaps'&#93;))
			{
				foreach(get_users() as $user)
					{
						$user = new WP_User($user->ID);
						$user->add_cap('access_s2member_ccap_members_only');
						// $user->remove_cap('access_s2member_ccap_additional_access');
					}
				exit;
			}
	}

Now run this by visiting:
http://www.example.com/?s2_update_customer_ccaps=yes


See also: http://codex.wordpress.org/Function_Reference/get_users
See also: http://codex.wordpress.org/Class_Reference/WP_User

Posted: Saturday Jan 19th, 2013 at 11:14 pm #38374

Hi Jason,

Thanks for all your suggestions. I think I’ve set up the framework, now I just need to do the leg work. I’ll let you know if anything comes up. Thanks so much!

Alan

Viewing 6 replies - 1 through 6 (of 6 total)

This topic is closed to new replies. Topics with no replies for 2 weeks are closed automatically.

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Contacting s2Member: Please use our Support Center for bug reports, pre-sale questions & technical assistance.