Scripting - Advanced PHP Conditionals

Please note this section is for advanced users you will need to employ the services of a developer familiar with PHP if you do not understand these functions.  

Remember OptimizePress combined with OptimizeMember provides many of these features available through our user friendly interface without the need for any custom code.
In an effort to give you even more control over access restrictions, optimizeMember makes some PHP functions, and also some PHP Constants, available to you from within WordPress.
In this section, we'll demonstrate several functions: is_user_logged_in(), is_user_not_logged_in(), user_is(user_id, role), user_is_not(user_id, role), user_can(user_id, capability), user_cannot(user_id, capability), current_user_is("role"), current_user_is_not("role"), current_user_can("capability"), current_user_cannot("capability"), current_user_is_for_blog($blog_id,"role"), current_user_is_not_for_blog($blog_id,"role"), current_user_can_for_blog($blog_id,"capability"), & current_user_cannot_for_blog($blog_id,"capability").
To make use of these functions, please follow our PHP code samples below. Using PHP, is a very powerful way to build Advanced Conditionals within your content; based on a Member's Level, Custom Capabilities, and/or other factors. In order to use PHP scripting inside your Posts/Pages, you'll need to install this handy plugin ( PHP Execution -  http://wordpress.org/plugins/php-execution-plugin/ ).

Example #1: Full access for anyone that is logged in.

<?php if(is_user_logged_in()){ ?><br>    Content for anyone that is logged in, regardless of their Membership Level.<br><?php } else { ?><br>    Some public content.<br><?php } ?>


Example #2: Full access for any Member with a Level >= 1.

<br><?php if (current_user_can("access_optimizemember_level1")){ ?><br>    Some content for Members who are logged in with an optimizeMember Level >= 1.<br><?php } else { ?><br>    Some public content.<br><?php } ?>


Example #3: Specific content for each different Member Level.

<?php if (current_user_is("optimizemember_level4")){ ?><br>    Some premium content for Level 4 Members.<br><?php } else if (current_user_is("optimizemember_level3")){ ?><br>    Some premium content for Level 3 Members.<br><?php } else if (current_user_is("optimizemember_level2")){ ?><br>    Some premium content for Level 2 Members.<br><?php } else if (current_user_is("optimizemember_level1")){ ?><br>    Some premium content for Level 1 Members.<br><?php } else if (current_user_is("optimizemember_level0")){ ?><br>    Some content for Free Subscribers.<br><?php } else { ?><br>    Some public content.<br><?php } ?>

Example #4: Using optimizeMember API Conditionals, supplementing WordPress core functions.

<?php if (current_user_is("optimizemember_level1")){ ?><br>    Content for Members at exactly Level# 1.<br><?php } else if(current_user_is_not("optimizemember_level4")) { ?><br>    Some content for those who are NOT at Level #4.<br><?php } else if(is_user_not_logged_in()) { ?><br>    Some public content.<br><?php } ?><br>


Example #5: Using optimizeMember API Conditionals, supplementing WordPress core functions.

<?php if (current_user_cannot("access_optimizemember_level1")){ ?><br>    Content for Members who CANNOT access Level #1 on this Blog.<br><?php } else if(current_user_cannot_for_blog(24, "access_optimizemember_level1")) { ?><br>    Content for Members who CANNOT access Level #1 on Blog ID# 24. ( i.e. Multisite Networking )<br><?php } else if(is_user_not_logged_in()) { ?><br>    Some public content.<br><?php } ?>


Example #6: Using multiple Conditionals together, and even nesting Conditionals.

<?php if(current_user_cannot("access_optimizemember_level4") && (current_user_can("access_optimizemember_level2") || current_user_can_for_blog(24, "access_optimizemember_level2"))) { ?><br>    <br>    This Member CANNOT access Level #4, but...<br>    they CAN access Level #2 on this Blog, OR (||) on Blog ID# 24. ( i.e. Multisite Networking )<br>    <br>    <?php if(current_user_can("access_optimizemember_ccap_free_gift")){ ?><br>        Also display free gift here. This is a Custom Capability check, using a nested Conditional.<br>    <?php } ?><br>    <br><?php } else if(current_user_can("access_optimizemember_level1") || current_user_can_for_blog(24, "access_optimizemember_level1")) { ?><br><br>    Content for Members who can access Level #1 on this Blog, OR (||) on Blog ID# 24. ( i.e. Multisite Networking )<br><br><?php } else if(is_user_not_logged_in()) { ?><br><br>    Some public content.<br><br><?php } ?>


Example #7: Using optimizeMember API Constants, instead of conditional functions.

<?php if (OPTIMIZEMEMBER_CURRENT_USER_IS_LOGGED_IN_AS_MEMBER){ ?><br>    Content for Members with an optimizeMember Level >= 1.<br><?php } else if(OPTIMIZEMEMBER_CURRENT_USER_IS_LOGGED_IN) { ?><br>    Some content for Free Subscribers.<br><?php } else { ?><br>    Some public content.<br><?php } ?>


Example #8: Using optimizeMember API Constants, instead of conditional functions.

<?php if (OPTIMIZEMEMBER_CURRENT_USER_ACCESS_LEVEL >= 4){ ?><br>    Some content for Members with an optimizeMember Level >= 4.<br><?php } else if (OPTIMIZEMEMBER_CURRENT_USER_ACCESS_LEVEL >= 3){ ?><br>    Some content for Members with an optimizeMember Level >= 3.<br><?php } else if (OPTIMIZEMEMBER_CURRENT_USER_ACCESS_LEVEL >= 2){ ?><br>    Some content for Members with an optimizeMember Level >= 2.<br><?php } else if (OPTIMIZEMEMBER_CURRENT_USER_ACCESS_LEVEL >= 1){ ?><br>    Some content for Members with an optimizeMember Level >= 1.<br><?php } else if(OPTIMIZEMEMBER_CURRENT_USER_ACCESS_LEVEL === 0){ ?><br>    Some content for Free Subscribers.<br><?php } else if(OPTIMIZEMEMBER_CURRENT_USER_ACCESS_LEVEL === -1){ ?><br>    Some public content.<br><?php } ?>

Membership Levels provide incremental access:

* A Member with Level 4 access, will also be able to access Levels 0, 1, 2 & 3.
* A Member with Level 3 access, will also be able to access Levels 0, 1 & 2.
* A Member with Level 2 access, will also be able to access Levels 0 & 1.
* A Member with Level 1 access, will also be able to access Level 0.
* A Subscriber with Level 0 access, will ONLY be able to access Level 0.
* A public Visitor will have NO access to protected content.

* WordPress Subscribers are at Membership Level 0. If you're allowing Open Registration, Subscribers will be at Level 0 ( a Free Subscriber ). WordPress Administrators, Editors, Authors, and Contributors have Level 4 access, with respect to optimizeMember. All of their other Roles/Capabilities are left untouched.