Membership 2 Pro template functions

Hi,

Are there functions available that I can use in template files and if so, is there documentation on them? For example, is there a function that I can use to get the membership id of the current logged in user? Or even better, is there a function where I can pass in the id of a media item and it will return true or false if the current user has permission to view it?

Thanks very much,

Jeremy

  • Ash

    Hi there

    You can try the following code:

    function get_membership_of_user( $user_id = 0 ) {
    	$user_id = $user_id == 0 ? get_current_user_id() : $user_id;
    	$all_subscriptions = MS_Model_Relationship::get_subscriptions(
    		array(
    			'user_id' => $user_id,
    			'status' => 'all',
    			'meta_key' => 'expire_date',
    			'orderby' => 'meta_value',
    			'order' => 'DESC',
    		)
    	);
    
    	$memberships = array();
    	foreach ( $all_subscriptions as $subscription ){
    		$membership = $subscription->get_membership();
    		$memberships[] = $membership->get_name_tag();
    	}
    
    	return implode( ' ', $memberships );
    }

    Now if you need membership of an user whose ID is 9, use like this:

    echo get_membership_of_user( 9 );

    If you want membership of current logged in user:

    echo get_membership_of_user();

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

  • Philipp Stracker

    Hi Jeremy,

    there are a few API functions that you can use to code your theme:
    They are documented inside the M2 plugin, just go to the page "Membership 2 > Help > API Docs".

    You'll possibly want to use this code to find out the current users membership:

    $api = ms_api();
    $membership = $api->detect_membership();

    There is no function yet to check if a certain page/media-item is available or not, but I've added this suggestion to our roadmap. It absolutely makes sense!

    Thanks for the feedback/suggestion, and hope the above code + documentation will help you.
    Philipp :slight_smile:

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.