How to check if a user belongs to a subscription level?

Hi, I want to know how to evaluate if a user has the subscription A or B active.

For example, I want to display a button to save a post only to users from the membership B.

if( user_has_paid_membership_b ){
display button
}

I just need a php method or constant, which returns a boolean value. I searched in the code, but I couldn´t find it. I only found a protected method from a class, which i can´t call from the outside.

    Vinod Dalvi

    Hi Jose,

    I hope you are well today and thank you for your question.

    You can either wrap the button code in the Membership 2 Pro membership shortcode like following where the ID 465 is the id of membership B.

    [ms-protect-content id="465"]
    display button
    [/ms-protect-content id="465"]

    To use the above shortcode in the template file, you have to display the shortcodes using do_shortcode function.

    Alternatively you can achieve this by developing custom code as described in the following reply.

    https://premium.wpmudev.org/forums/topic/get-a-members-membership-level#post-890804

    Best Regards,
    Vinod Dalvi

    Jose Vega

    Hey , I created a few php helper functions to retrieve user memberships and validate if a user has a specific membership active.

    I hope they´re helpful for you guys.

    /**
     * Get user active memberships names.
     * @param int $user_id user id
    * @return array|boolean
     */
    function get_user_active_memberships_names($user_id = null) {
    
    	$memberships = get_user_active_memberships($user_id);
    	if (!$memberships) {
    		return false;
    	}
    	$names = array();
    	foreach ($memberships as $membership) {
    		$names[] = $membership->name;
    	}
    	return $names;
    }
    
    /**
     * Get user active memberships ids.
     * @param int $user_id user id
    * @return array|boolean
     */
    function get_user_active_memberships_ids($user_id = null) {
    
    	$memberships = get_user_active_memberships($user_id);
    	if (!$memberships) {
    		return false;
    	}
    	$memberships_ids = array();
    	foreach ($memberships as $membership) {
    		$memberships_ids[] = $membership->id;
    	}
    	return $memberships_ids;
    }
    /**
    * Check if a user is member of a specific membership.
    * @param str|int $membership membership name or id
    * @param int $user_id
    * @return array|boolean
    */
    function is_user_member_of($membership, $user_id = null) {
    
    	if( ! class_exists('MS_Model_Member')){
    		return false;
    	}
    	$memberships = ( is_int($membership) ) ? get_user_active_memberships_ids( $user_id ) : get_user_active_memberships_names( $user_id );
    
    	if(is_user_logged_in() &&  empty( $memberships ) && $membership === self::LEVEL1_NAME ){
    		return true;
    	}
    	if (is_user_logged_in() && ( MS_Model_Member::is_admin_user() or ( is_array($memberships) && in_array($membership, $memberships) ))) {
    		return true;
    	}
    	return false;
    }
    /**
    * Get user active memberships, including trials.
    * @param int $user_id
    * @return array|boolean
    */
    function get_user_active_memberships($user_id = null) {
    
    	if (!is_user_logged_in()) {
    		return false;
    	}
    	if (!$user_id) {
    		$user_id = get_current_user_id();
    	}
    	if (class_exists('MS_Model_Relationship')) {
    		$subscriptions = MS_Model_Relationship::get_subscriptions(
    						array(
    							'user_id' => $user_id,
    							'status' => 'all',
    						)
    		);
    		$memberships = array();
    		if (!is_array($subscriptions)) {
    			return false;
    		}
    		foreach ($subscriptions as $subscription) {
    			// Do not display system-memberships in Account
    			if ($subscription->is_system()) {
    				continue;
    			}
    			if ($subscription->get_status() == MS_Model_Relationship::STATUS_ACTIVE or $subscription->get_status() == MS_Model_Relationship::STATUS_TRIAL) {
    				$memberships[] = $subscription->membership;
    			}
    		}
    		return $memberships;
    	}
    	return false;
    }