Getting membership level for a particular user

Hello

I'm using Buddypress 1.2.8 with the Membership 2.0.4 plugin. I'm trying to filter and sort the Buddypress Members directory by membership level.

Does Membership have a built-in function to return any user's level id?

I looked for something like "get_member_level()" but couldn't find it. I was able to get the membership level for a particular user (not the logged-in user) by doing this in the while ( bp_members() :...) loop in the templates:

$member_id = bp_get_member_user_id();
$is_premium = user_is_premium($member_id);

if (user_is_premium($member_id)){
....do stuff with user profile info in members directory...
}

I put the user_is_premium function in a plugin:

# adapted from function current_user_on_level( $level_id )
# in membership/membershipincludes/includes/functions.php
function user_is_premium($member_id){
$member = new M_Membership( $member_id );
if(!empty($member)) {
return $member->on_level( '3', true );
} else {
return false;
}
}

It seems to work fine. I'm just wondering if there's a simpler/cleaner way to do it using Membership functions.

thanks!

  • Jonathan
    • The Incredible Code Injector

    Here is another thread on the subject,
    https://premium.wpmudev.org/forums/topic/checking-member-level-via-php

    Here is the code you're after for the membership plugin :slight_smile:
    current_user_is_member()
    current_user_has_subscription()
    current_user_on_level($level_id)
    current_user_on_subscription($sub_id)

    Regards,
    Jonathan

    Edit: you will find them in membership/membershipincludes/includes/functions.php
    Hope that helps :slight_smile:

  • cparkinson
    • Flash Drive

    Thanks, Jonathan!

    I should have specifically said that I was looking for how to find ANY users membership level given their user id. Finding the current user's level doesn't help me with the members directory - I'm trying to change the display based on the membership level of the users who are being shown in the search results. For example, I'd like to display certain profile fields for membership level 4, and leave out everyone who's membership level 3.

    Those current_user functions look really handy for some of my other problems, though. Thanks!

  • Jonathan
    • The Incredible Code Injector

    Sorry cparkinson,

    I should have read more carefully - lol.

    Your code works - and mirrors the functions i listed. So Yes, your code is clean enough. laugh.

    Here is a little simpler/cleaner way...
    Usage: user_is_on_premium_level(10534, 2) returns true / false 1 / null

    function user_is_on_premium_level($user_id, $level_id) {
    
    	$member = new M_Membership( $user_id );
    
    	if(!empty($member)) {
    		return $member->on_level( $level_id, true );
    	} else {
    		return false;
    	}
    
    }

    Usage: user_is_on_premium_sub(10534, 1) returns true / false 1 / null

    function user_is_on_premium_sub($user_id, $sub_id) {
    
    	$member = new M_Membership( $user_id );
    
    	if(!empty($member)) {
    		return $member->on_sub( $sub_id );
    	} else {
    		return false;
    	}
    
    }

    Hope that helps a bit :wink:

    Regards,
    Jonathan

  • Barry
    • DEV MAN’s Mascot

    Hi - yeah the functions in functions.php will provide a guide to using the member class methods.

    One of the handier functions to use is current_member() as an example:

    $member = current_member();
    if(member->on_level(21))
    return true;
    else
    return false;
  • DavidM
    • DEV MAN’s Mascot

    Hi guys,

    I meant to say in my previous response that I hadn't seen a function that yields the subscription or membership level based on a given user_id!

    Being awesome as the developers around here are, Barry's been so kind as to provide us with the following snippet for just that!

    function get_sub_by_user($user_id) {
    
    	$member = new M_Membership( $user_id );
    	if($member->has_subscription()) {
    		// note this could return more than one subscription so may want to check it's an array and then return the first one if only want one result
    		return $member->current_subscription();
    	} else {
    	return false;
    	}
    }

    EDIT: Let's wrap that in a function!

    Cheers!
    David

  • jeremy_bass0
    • Flash Drive

    For others seraching, I needed to get the name of the subscription of the user, but the top level. This is what I used.

    //I put this in the function.php of the theme
    function get_sub_by_user($user_id) {
    
    	$member = new M_Membership( $user_id );
    	if($member->has_subscription()) {
    		// note this could return more than one subscription so may want to check it's an array and then return the first one if only want one result
    		return $member->current_subscription();
    	} else {
    	return false;
    	}
    }
    
    //this in the theme where ever needed
    //outputs the name of the Subscription
    $subscription = new M_Subscription(end(get_sub_by_user($user_id))->sub_id);
    echo $subscription->sub_name();

    Hope that helps someone else looking for this question.

  • Kaxa
    • Design Lord, Child of Thor

    Hello

    I tried using Jeremy_Bass0's Solution, but it gives me a following error:

    Warning: end() [function.end]: Passed variable is not an array or object in .....wp-content/themes/my-theme/category.php on line 129

    Can anyone tell me how to fix this please?

    I basically Copy - Pasted the first part of the code in my theme function.php

    and then Copy - Pasted the second part of the code in my category.php to echo / display on my category page.

  • DavidM
    • DEV MAN’s Mascot

    Hi Kaxa,

    I'm assuming your line 129 reads thusly?

    $subscription = new M_Subscription(end(get_sub_by_user($user_id))->sub_id);

    If so, have you defined $user_id prior to that, like so (if not, could you try adding this right before that other line)?

    $user_id = get_current_user_id();

    And if that's already been done, could you try echoing just that function, as follows, to see what it displays?
    echo get_sub_by_user($user_id);

    Adding that $user_id definition might just do the trick. But if it's already defined then it'd help to know exactly what the function is returning.

  • Kaxa
    • Design Lord, Child of Thor

    David M, You are my Hero! : ))

    Thanks, its working fine.

    The only problem I see now, is that if a member is not registered, or not logged in, he sees following error code:

    Warning: end() [function.end]: Passed variable is not an array or object in my-theme/category.php on line 132

    My Code Snippet that displays the Subscription Plan Name:

    <?php 
    
    $user_id = get_current_user_id();
    $subscription = new M_Subscription(end(get_sub_by_user($user_id))->sub_id);
    echo $subscription->sub_name();
    
    ?>

    category.php on line 132 << With the problem is:
    $subscription = new M_Subscription(end(get_sub_by_user($user_id))->sub_id);

    Can you advise how can I fix this, so that it will not display that error and display some kind of custom note instead?

    Thanks again!

  • Kaxa
    • Design Lord, Child of Thor

    David M, You are my Hero! : ))

    Thanks, its working fine.

    The only problem I see now, is that if a member is not registered, or not logged in, he sees following error code:

    Warning: end() [function.end]: Passed variable is not an array or object in my-theme/category.php on line 132

    My Code Snippet that displays the Subscription Plan Name:

    <?php 
    
    $user_id = get_current_user_id();
    $subscription = new M_Subscription(end(get_sub_by_user($user_id))->sub_id);
    echo $subscription->sub_name();
    
    ?>

    category.php on line 132 << With the problem is:
    $subscription = new M_Subscription(end(get_sub_by_user($user_id))->sub_id);

    Can you advise how can I fix this, so that it will not display that error and display some kind of custom note instead?

    Thanks again!

  • DavidM
    • DEV MAN’s Mascot

    If I understand you correctly, you can just wrap that piece of code with the is_user_logged_in() function.

    http://codex.wordpress.org/Function_Reference/is_user_logged_in

    <?php 
    
    if (is_user_logged_in()){
    $user_id = get_current_user_id();
    $subscription = new M_Subscription(end(get_sub_by_user($user_id))->sub_id);
    echo $subscription->sub_name();
    } else {
    echo 'Please register or log in to see more.'
    }
    
    ?>

    That 'else' statement is optional but lets you display whatever you want to users who aren't logged in, via the 'echo' statement.

  • jzvestor
    • Site Builder, Child of Zeus

    For others searching for how to show specific content to a specific level from the template, I used this very simple version:
    `<?php

    if (current_user_on_level(2)){
    echo 'show logged in stuff';
    } else {
    echo 'Show logged out stuff';
    }

    ?>

    Boom.

  • DavidM
    • DEV MAN’s Mascot

    @vusis, @Randolf,

    Sorry I missed the question earlier, but Barry mentions a couple functions in the following thread for setting levels and subscriptions.

    https://premium.wpmudev.org/forums/topic/programmatically-setting-a-level

    To make things quicker for ya, here are the two functions:

    function myaddusertosub( $user_id, $sub_id ) {
    
    $member = new M_Membership($user_id);
    $member->create_subscription( $sub_id );
    
    }
    function myaddusertolevel( $user_id, $level_id ) {
    
    $member = new M_Membership($user_id);
    $member->add_level( $level_id );
    
    }

    Hope that helps!

  • Luis
    • WPMU DEV Initiate

    Since Membership API has a few changes I took @jeremy_bass0 function and made some changes:

    function get_sub_by_user( $user_id )
    {
        $member = new Membership_Model_Member( $user_id );
        if ($member->has_subscription()) {
            $subids = $member->get_subscription_ids();
    
            $subscription = [];
            foreach($subids as $subId) {
                $sub = new Membership_Model_Subscription($subId);
    
                $subscription[] = [
                    'name' => $sub->sub_name(),
                   //  'expire_on' => get_subscription_expire_date($subId)
                ];
            }
    
            return $subscription;
        } else {
            return false;
        }
    }

    Just one doubt, how do I get the expire date for each subscription? This is the only part I'm missing on my code, any advice or help?

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.