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.

Code to target any capability

Home Forums Community Forum Code to target any capability

This topic contains 1 reply, has 2 voices. Last updated by  David Welch 4 years, 9 months ago.

Topic Author Topic
Posted: Monday Apr 2nd, 2012 at 9:42 am #9761

Hi all,

I am using s2member as a basic client portal system, no payment involved. Was wondering if there was a conditional statement to target members with ANY capability. I basically want to add some links such as below. The highlights show where I mean but it doesn’t work and I cant find any conditional that fits the bill:

<ul>

&lt;?php if ( is_user_logged_in() &amp;&amp; current_user_can(&quot;<strong>access_s2member_ccap</strong>")) { ?&gt;

<li>&lt;a href=&quot;"&gt;Log out</a></li>
<li><a href="/clients/client-portal/">Public Client Documents</a></li>
<li>&lt;a href=&quot;/clients/client-portal/"&gt;My Private Documents</a></li>

&lt;?php } else if ( is_user_logged_in() &amp;&amp; current_user_cannot(&quot;<strong>access_s2member_ccap</strong>")) { ?&gt;

<li>&lt;a href=&quot;"&gt;Log out</a></li>
<li><a href="/clients/client-portal/">Public Client Documents</a></li>

 

<li>&lt;a href=&quot;"&gt;Log in</a></li>
<li><a href="#">Register</a></li>



</ul>

The first set of links I’d like to show for members who are logged-in AND have a capability set (regardless of what the capability is), the second set of links is for anyone who is logged in but who don’t have a capability, and the last set is for anyone not logged-in.

Would anyone know if this can be done and how to achieve it?

Thanks in advance,
Rob

List Of Topic Replies

Viewing 1 replies (of 1 total)
Author Replies
Author Replies
Posted: Tuesday Apr 3rd, 2012 at 5:24 pm #9969
David Welch
Username: dwbiz05

I don’t know of a native s2member function for this, but you could create your own shortcode filter by putting something like this in your functions.php file or your s2hacks.php file (if you have one in the mu-plugins directory).

The main issue is that the capabilities are all stored in the same wp_capabilities serialized array in the database. So you have to filter through that some how. To me, the easiest is to just pull the user meta and parse through it like so:

<?php
function any_cap_check($atts, $content){	
	$cc = get_user_meta(S2MEMBER_CURRENT_USER_ID, 'wp_capabilities',true); // Gets the user's role
	if($cc != false){ //False means the user isn't logged in or it can't pull the capabilities.
		foreach($cc as $k => $v){
			if($k == 'administrator'){	//renders for admins
				return do_shortcode($content);
				break;
			}
			else if(strstr($k,'access_s2member_ccap')){ //Renders if the user has ANY custom capabilities with s2member.
				return do_shortcode($content);
				break;
			}
		}
	}
}

add_shortcode('check-any-cap','any_cap_check'); //Creates the shortcode function.
?>

You could call it then with the shortcode in your post or page like so:

[check-any-cap]
Your content here.
[/check-any-cap]

Hope that helps,

Dave

EDIT: you can manipulate that code and create a second one or do some different filtering by passing variables to choose if you want any or no capabilities to show, but that is the basic way to create a shortcode for what you want.

  • This reply was modified 4 years, 9 months ago by  David Welch.
  • This reply was modified 4 years, 9 months ago by  David Welch.
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.