Paid Memberships Pro Condition for Oxygen

,

YouTube video

Written By

Jonathan Jernigan

If you’re looking to build a membership site with Oxygen using Paid Memberships Pro, you’ll be happy to learn that it works perfectly fine.

In my case during a recent client site build, we needed extra functionality when building the site to protect content on the site built in Oxygen builder, instead of just the default WordPress content editor. This website required the ability to show and hide certain sections, buttons, videos, images, etc based on whether or not the user had the correct membership level.

For instance, if a “tutorial” was set to only allow Premium members, then those that were Classic members should not see the tutorial content, they should see a different section with sales copy and a teaser video with buttons to upgrade their membership. It also applies to those users that were logged out, but instead of seeing an upgrade membership button, they would see a “become a member” button.

Here is the code. Add this as a code snippet using the Code Snippets or WPCodeBox plugin. You need to replace the example membership levels of  “Free”, “Classic”, or “Premium” with the name of your membership levels from Paid Memberships Pro.

//Get array of membership levels required for access to current post.
function av_get_current_post_levels() {
    
    $post_ID = get_the_ID();

    global $membership_levels, $wpdb;
    $post_levels = $wpdb->get_col("SELECT membership_id FROM {$wpdb->pmpro_memberships_pages} WHERE page_id = '{$post_ID}'");
    $protected_levels = array();
    foreach($membership_levels as $level)
    {
        $protectedcategories = $wpdb->get_col("SELECT category_id FROM $wpdb->pmpro_memberships_categories WHERE membership_id = $level->id");
        if(
            in_array( $level->id, $post_levels ) ||
            in_category($protectedcategories, $post_ID)
        ) {
            $protected_levels[] = $level->name;
          }
    }
    
    return $protected_levels;
    
}

//Get name of current user's membership level.
function av_get_current_user_level() {
    
    if( is_user_logged_in() && function_exists('pmpro_hasMembershipLevel') && pmpro_hasMembershipLevel() )
    {
        global $current_user;
        $current_user->membership_level = pmpro_getMembershipLevelForUser($current_user->ID);
        return $current_user->membership_level->name;
    } else { return null; }
    
}

// Register functions within this if( function_exists ) block.
if( function_exists('oxygen_vsb_register_condition') ) {

//Register "User Cannot Access" function, used for showing lock icons on tutorials archive.
oxygen_vsb_register_condition('Cannot Access', array('options'=>array(), 'custom'=>true), array('--'), 'lock_logic_callback', 'PMPRO');

function lock_logic_callback($value, $operator) {
    
    // It's important to note that the question we're answering true or false to
    // is "Can the user NOT access this?" e.g. false would mean they can access it,
    // whereas true means they cannot access it.
    
    // If the user isn't logged in, always return false
    if( !is_user_logged_in() ) { return false; }
    
    // Get the post membership level requirement
    $post_mem_lvl = av_get_current_post_levels();
    
    //Might need to add "Free" to the array instead of just changing it to a string
    if( count($post_mem_lvl) < 1 ) { $post_mem_lvl = "Free"; }
    
    //Check user level
    $user_level = av_get_current_user_level();
    
    if( !$user_level ) { $user_level = "Free"; }
    
    // Never show a lock icon to a "Premium" user.
    if( $user_level == "Premium" ) { return false; }
    
    // Never show a lock icon on a tutorial set to "Free"
    if( $post_mem_lvl == "Free" ) { return false; }
    
    // Never show a lock icon if user level = any level assigned to the post
    if( in_array( $user_level, $post_mem_lvl ) ) { return false; };
    
    // Show lock icon on "premium only" tutorials if user is "classic"
    if( in_array( "Premium", $post_mem_lvl ) && !in_array( "Classic", $post_mem_lvl ) && $user_level == "Classic" ) { return true; }
    
    //If they're classic, show lock icon on premium tutorials
    //if( $post_mem_lvl == "premium"  && $user_level == "classic" ) { return true; }
    
    // Show a lock icon on premium & classic tutorials if user level is Free"
    if( in_array( "Premium", $post_mem_lvl ) || in_array( "Classic", $post_mem_lvl ) && $user_level == "Free" ) { return true; }

}
    
//Grabbing the Oxygen condition operators helper
global $oxy_condition_operators;
    
// Simple user membership level condition, "User Is Level"
oxygen_vsb_register_condition('User Level', array('options'=>array(), 'custom'=>true), $oxy_condition_operators['string'], 'user_level_callback', 'PMPRO');
    
function user_level_callback( $value, $operator ) {
    
    $user_level = av_get_current_user_level();
    
    if( !$user_level ) { $user_level = "Free"; }

    return oxy_condition_eval_int($user_level, $value, $operator);
    
}
    
// Simple access check, "Access Check"
// It will hide content the user doesn't have the membership level for.
oxygen_vsb_register_condition('Can Access', array('options'=>array(), 'custom'=>true), array('--'), 'access_check_callback', 'PMPRO');
    
function access_check_callback() {
    
    $user_level = av_get_current_user_level();
    $post_level = av_get_current_post_levels();
    
    if( count( $post_level ) < 1 ) { return true; }
    else if( in_array( $user_level, $post_level ) ) { return true; }
    else { return false; }
    
}
    
}

Signup for the most inconsistent newsletter this side of the Mississippi

Delivered on a regular-as-I-can basis, I'll share with you the tl;dr of new blog posts and videos, exciting announcements, and other valuable information from around the WordPress ecosphere. You'll never get more than one email per week from me.

"*" indicates required fields

This field is for validation purposes and should be left unchanged.