Search for answers:
The OptimizePress Support Site Has Moved: Click here to access the new Support site

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()){ ?>
    Content for anyone that is logged in, regardless of their Membership Level.
<?php } else { ?>
    Some public content.
<?php ?>

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

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


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

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


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

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

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

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

    Content for Members who can access Level #1 on this Blog, OR (||) on Blog ID# 24. ( i.e. Multisite Networking )

<?php } else if(is_user_not_logged_in()) { ?>

    Some public content.

<?php ?>


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

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

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

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

Didn't find an answer to your question? Contact Our Support Team (Submit a ticket)
Was this article helpful?
0 out of 0 found this helpful
Follow

Comments

Other Help Categories:

Get Started Guide

Watch our Ultimate Getting Started guide video

Pre-Sales Questions

Ask questions before purchasing our products

Login Problems

If you're having issues logging into our members hub

Accounts & Billing

Update your billing, request a refund and other accounts help

API Key Issues

Get help with licensing your site and API key issues

Membership Help

Get help creating Membership portals and courses

LiveEditor Help

If you need help using or troubleshooting the LiveEditor

Integrations

Find out about the many integrations possible with OptimizePress

Affiliate Questions

Have questions about promoting OptimizePress

Clubhouse Questions

Questions about our Clubhouse Membership Subscription