Checking for membership level of subscriber by user ID

Hi, I'm trying to modify the buddypress member directory loop to show the membership level of each user. Due to the funny way the membership loop seems to work I need to define the user ID within the loop (so current_user_is_member() / current_user_on_level($level_id) won't work).
In another forum post (https://premium.wpmudev.org/forums/topic/getting-membership-level-for-a-particular-user) a function was provided:

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;
	}
}

However, M_Membership::current_subscription() is showing an error for me (undefined method). Is this still the best way to do this?

PS. Feature suggestion - could you put dates on peoples post within forum topics? Its tough to figure out how up to date a forum discussion is...

Thanks for any help with this!

  • Patrick

    Hey there @mike

    Welcome to the forums, glad to have you aboard!

    Here's a recent thread where I and my colleague @Ashok had the occasion to help out another member with a similar issue:
    https://premium.wpmudev.org/forums/topic/display-membership-data-printmobile#post-713020

    There's a custom-built plugin in that post that outputs the logged-in user's name, email, username & Membership level via a shortcode.

    You should be able to use the code for your purpose in the BuddyPress activity stream too.

    Please do let us know if this helps, and don't hesitate to post again if you need to.

    Thanks!

  • mike

    Thanks, that nailed it.
    For reference, I used this:

    function get_sub_by_user( $user_id ) {
    
    	$factory = Membership_Plugin::factory();
    	$user_object = $factory->get_member( $user_id );
    	$userlevels = $user_object->get_level_ids();
    
    	if (!empty($userlevels)) {
    		$rows = array();
    		foreach ((array) $userlevels as $key => $value) {
    			$level = Membership_Plugin::factory()->get_level($value->level_id);
    			if (!empty($level)) {
    				if ((int) $value->sub_id != 0) {
    					$rows[] = "<strong>" . $level->level_title() . "</strong>";
    				} else {
    					$rows[] = $level->level_title();
    				}
    			}
    		}
    		return $rows;
    	} else {
    		return false;
    	}
    }

    Also, I've noticed that now I'm logged in the posts have dates! My bad, I didn't notice the change.

    Thanks!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.