wpsharks / s2member-kb

The s2Member® and s2Member® Pro Knowledge Base
9 stars 4 forks source link

Current User Conditionals via PHP code #295

Open kristineds opened 8 years ago

kristineds commented 8 years ago

KB Article Creation Checklist

Additional TODO list items go here.

title: Current User Conditionals via PHP code
categories: Tutorials
tags: api-scripting,mu-plugins-hacks
author: kristineds
github-issue: https://github.com/websharks/s2member-kb/issues/295

This is a work in progress.


[intro]


Using the current_user_cannot() Function

s2Member adds a new function to WordPress called current_user_cannot(). This is a special addition provided by s2Member that makes false testing easier for some. This conditional is used to determine if the current User does NOT have a specific Capability or Role.

This function accepts two arguments:

<?php
if(current_user_can("access_s2member_level0"))
    echo 'You CAN access content protected at Level #0.';
else if(current_user_cannot("access_s2member_level0"))
    echo 'You CANNOT access content protected at Level #0.';
?>

_See also: WordPress Core Function current_user_can()_


Using the current_user_cannot_for_blog() Function

This is a new function to WordPress added by s2Member called current_user_cannot_for_blog(). This conditional will determine if the current User does NOT have a specific Capability or Role, on a specific Blog within a Multisite Network.

This function accepts three arguments:

<?php
if(current_user_can_for_blog(5, "access_s2member_level0"))
    echo 'You CAN access content protected at Level #0 (on Blog ID 5).';
else if(current_user_cannot_for_blog(5, "access_s2member_level0"))
    echo 'You CANNOT access content protected at Level #0 (on Blog ID 5).';
?>

_See also: WordPress Core Function current_user_can_for_blog()_


Using the current_user_is() Function

A new function added to WordPress by s2Member called current_user_is(). This conditional will determine if the current User is/has a specific Role.

This function accepts this argument:

<?php
if(current_user_is("subscriber"))
    echo 'You ARE a Free Subscriber at Level #0.';
else if(current_user_is("s2member_level1"))
    echo 'You ARE a Member at Level #1.';
else if(current_user_can("access_s2member_level2"))
    echo 'You DO have access to content protected at Level #2.';
    # But, (important) they could actually be a Level #3 or #4 Member;
    # because Membership Levels provide incremental access.
?>

_See also: WordPress Core Function current_user_can()_


Using the current_user_is_for_blog() Function

A new function added to WordPress by s2Member called current_user_is_for_blog(). This conditional will determine if the current User is/has a specific Role, on a specific Blog within a Multisite Network.

This function accepts two arguments:

<?php
if(current_user_is("subscriber"))
    echo 'You ARE a Free Subscriber at Level #0 (on this Blog).';
else if(current_user_is_for_blog(5, "subscriber"))
    echo 'You ARE a Free Subscriber at Level #0 (on Blog ID 5).';
else if(current_user_is_for_blog(5, "s2member_level1"))
    echo 'You ARE a Member at Level #1 (on Blog ID 5).';
else if(current_user_can_for_blog(5, "access_s2member_level2"))
    echo 'You DO have access to content protected at Level #2 (on Blog ID 5).';
    # But, (important) they could actually be a Level #3 or #4 Member (on Blog ID 5);
    # because Membership Levels provide incremental access.
?>

_See also: WordPress Core Function current_user_can_for_blog()_


Using the current_user_is_not() Function

A new function added to WordPress by s2Member called current_user_is_not(). This conditional will determine if the current User is/does NOT have a specific Role.

This function accepts this argument:

<?php
if(current_user_is("subscriber"))
    echo 'You ARE a Free Subscriber at Level #0.';
else if(current_user_is("s2member_level1"))
    echo 'You ARE a Member at Level #1.';
else if(current_user_can("access_s2member_level2") && current_user_is_not("s2member_level2"))
    echo 'You DO have access to content protected at Level #2, but you are NOT a Level #2 Member.';
    # So, (important) they could actually be a Level #3 or #4 Member;
    # because Membership Levels provide incremental access.
?>

_See also: WordPress Core Function current_user_can()_


Using the current_user_is_not_for_blog() Function

A new function added to WordPress by s2Member called current_user_is_not_for_blog(). This conditional will determine if the current User is/does NOT have a specific Role, on a specific Blog within a Multisite Network.

This function accepts two arguments:

<?php
if(current_user_is_for_blog(5, "subscriber"))
    echo 'You ARE a Free Subscriber at Level #0 (on Blog ID 5).';
else if(current_user_can_for_blog(5, "access_s2member_level1") && current_user_is_not_for_blog(5, "s2member_level1"))
    echo 'You DO have access to content protected at Level #1 (on Blog ID 5), but you are NOT a Level #1 Member (on Blog ID 5).';
    # So, (important) they could actually be a Level #2 or #3 or #4 Member (on Blog ID 5);
    # because Membership Levels provide incremental access.
?>

_See also: WordPress Core Function current_user_can_for_blog()_


See also: [s2If /] Simple Shortcode Conditionals

jaswrks commented 8 years ago

Another function WordPress® Function → current_user_can_for_blog() already exists in the WordPress® core.

I think this statement would work better if it comes after the documentation for each function. For instance, maybe in italics we can just say something like:

_See also: WordPress Core Function current_user_can_for_blog()_