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.

How to Test for Custom Capabilities (example)

Home Forums Community Forum How to Test for Custom Capabilities (example)

This topic contains 1 reply, has 2 voices. Last updated by  Jason (Lead Developer) 3 years, 10 months ago.

Topic Author Topic
Posted: Friday Feb 15th, 2013 at 5:46 pm #41903
Randy King
Username: RandyKing

I need to know if a user has ANY custom capabilities so I can display some default text for a user that has no capabilities assigned. Here’s how to do that…

First, we know that the custom capabilities are inside the s2member “capabilities” array and we extract that array using the get_user_option() function. That array contains key-value pairs for, among other things, the custom capabilities.

For example, if the logged-in user has 2 custom capabilities “music” and “videos” assigned, I will use the following PHP code and the resulting array will conceptually look like this…

$usercaps = get_user_option('capabilities', S2MEMBER_CURRENT_USER_ID);
...
subscriber => 1
access_s2member_ccap_music => 1
access_s2member_ccap_videos => 1

Note above that when any custom capability is present, the sub-string “s2member_ccap” is present as a key somewhere in this array, at least once.

The next step is to extract the “key” portion of the key-value pairs because the “value” for a custom capability is always going to be “1”, meaning that “this custom capability is assigned”. So we use the PHP function “array_keys()” to extract an array of just the keys present in this array.

$userkeys = array_keys($usercaps);
...
subscriber
access_s2member_ccap_music
access_s2member_ccap_videos	

Next, we want to create a long string of all the keys in that array so that we can search for the presence of the “s2member_ccap” sub-string that will be present if any custom capability is assigned, so we use the PHP “implode()” function to create a comma-separated list of all those keys.

$userkeylist = implode(',' , $userkeys);
...
subscriber,access_s2member_ccap_music,access_s2member_ccap_videos	

Finally, we use the PHP “strpos()” function to search for that “s2member_ccap” sub-string. If this function returns a value, it is the position in the string where that sub-string first occurs. Here’s the important thing – if the sub-string is not found, strpos() returns a Boolean FALSE and that’s what we’re really looking for.

if (strpos($userkeylist, 's2member_ccap') === false)

One last PHP note – we use the ‘===’ operator for testing because there are cases where the numeric return value of strpos() could evaluate to a FALSE condition, and using that operator forces the comparison to test exactly for the Boolean type of false.

After that windy and detailed explanation, here’s the full code that would go on the page where you need it…

We're sorry; you have not registered for any of the products.

Note that the first 3 functions are all coded on one line as the return of one function feeds into the argument of the next function. Also, this is not the only way to do this; you can use other PHP constructs such as a FOREACH statement to iterate through the keys, etc. I copied the above code right out of the page where I am using it.

List Of Topic Replies

Viewing 1 replies (of 1 total)
Author Replies
Author Replies
Posted: Monday Feb 18th, 2013 at 4:38 pm #42340
Staff Member
Great article Randy. Thanks for posting this!

Here is another quick example. Not any better, but an alternative way.

<?php
if(($user = wp_get_current_user()))
	foreach ($user->allcaps as $_cap => $_cap_enabled)
		if (preg_match ('/^access_s2member_ccap_/', $_cap) && $_cap_enabled)
			$s2member_ccaps[] = preg_replace ('/^access_s2member_ccap_/', '', $_cap);
unset($_cap, $_cap_enabled); // Housekeeping.

if(empty($s2member_ccaps))
	echo 'Sorry; you have not registered for any of the products.';

Here is a built-in class method that will make this even easier.

http://www.s2member.com/codex/stable/s2member/user_access/c_ws_plugin__s2member_user_access/

Using built-in class method to check a specific User by ID#.

http://www.s2member.com/codex/stable/s2member/user_access/c_ws_plugin__s2member_user_access/


See also: Knowledge Base » s2Member® Roles/Capabilities
See also: Knowledge Base » Changing Roles/Capabilities via PHP
See also: Video » s2Member (Custom Capabilities)

Viewing 1 replies (of 1 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.