Get a members membership level

Is there a function that returns a users membership? I want to output a users membership level next to their name on my site.

I had a look in plugin folder but couldn't see any obvious template functions file.

Thanks

  • Mike
    • The Bug Hunter

    Hi Vinod,

    Thanks for the quick reply.

    Unfortunately that won't work as that will just work for the person viewing their own level.

    I want to get a users membership and then output it so everyone can read it. Like you have done in this forum. Next to my name it says "elite" and next to yours it says "staff".

    If you can give me some hints with functions to look at or where this is stored in database I would be very grateful.

  • Vinod Dalvi
    • WP Unicorn

    Hi Mike,

    Thank you for your reply.

    You can try using the following custom code to achieve this.

    if ( class_exists('MS_Model_Member') ) {
    	if ( MS_Model_Member::is_admin_user() ) {
    		_e( 'You are an admin user and have access to all memberships', MS_TEXT_DOMAIN );
    	}else{
    		$member = MS_Model_Member::get_current_member();
    
    		foreach ( $member->subscriptions as $subscription ) {
    			$membership = $subscription->get_membership();
    			echo $membership->name;
    		}
    	}
    }

    Best Regards,
    Vinod Dalvi

  • Mike
    • The Bug Hunter

    Hi Vinod,

    Thanks for the reply.

    That wouldn't work for the scenario I described as you wouldn't be able to view other peoples memberships (i.e. see it written next to their name in forum), however a quick look inside MS_Model_Member::get_current_member() let me follow the breadcrumbs.

    Instead of get_current_member() it looks like I can do:

    $memberships = MS_Model_Relationship::get_subscriptions( array( 'user_id' => $userId );

    Thanks for your help.

  • Vinod Dalvi
    • WP Unicorn

    Hi Mike,

    The provided code worked for me as the current user and you are right that the get_current_member used in the previous code will only work with the current user as it doesn't take any user ID as parameter.

    We can modify the previously provided code as following to make it work with other users also. you have to just set the user id for the variable $userId in the following code.

    if ( class_exists('MS_Model_Member') ) {
    	if ( MS_Model_Member::is_admin_user() ) {
    		_e( 'You are an admin user and have access to all memberships', MS_TEXT_DOMAIN );
    	}else{
    		$userId = get_current_user_id(); // Set the User ID here
    		$subscriptions = MS_Model_Relationship::get_subscriptions( array( 'user_id' => $userId ));
    		foreach ( $subscriptions as $subscription ) {
    			$membership = $subscription->get_membership();
    			echo $membership->name;
    		}
    	}
    }

    Cheers,
    Vinod Dalvi

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.