Access Membership outside of Theme Files

Hi,

I can access functions of your membership plugin from my Theme template files and from the functions.php file. However I have created a bespoke popup login form who's files are located outside of the wp-content folder which worked when I had the original membership plugin installed. However since updating to version 2 it seems I can call the function but it fails. I think it has to do with accessing the plugin files directly. Originally I used global $M_Membership and global $M_Options and wondered if this is the issue. I have tried changing to $Membership_Model_options and $Membership_Model_Member without success. Do you know how I can achieve this?

  • Vaughan

    Hi Paul,

    Hope you're well?

    I'm not sure if the API controller is available in that way, but if it can, you might find the following useful.

    $api = ms_api();
    $member = $api->get_current_member();
    ?
    // Check if current user is in certain membership:
    $membership_id = 100; // hardcode the ID.
    $membership_id = $api->get_membership_id( 'premium' ); // fetch by membership name.
    if ( $member->has_membership( $membership_id ) ) {
    	echo "You are member of " . $membership_id;
    }
    ?
    ?
    // Get the membership of the current user:
    $membership = $api->detect_membership();
    echo "First membership: " . $membership->name;
    // Note: If multiple-memberships is enabled, this will return only the first membership!
    ?
    ?
    // Get all memberships of the current user (multiple memberships)
    foreach ( $member->subscriptions as $subscription ) {
    	// Note: Double check if user has_membership() to filter out expired/pending subscriptions
    	if ( ! $member->has_membership( $subscription->membership_id ) ) {
    		continue;
    	}
    
    	$membership = $subscription->get_membership();
    	echo "<br>Your membership: " . $membership->name;
    }

    You can find the controller here, it might help you achieve your goal as it's quite well documented in the code.

    /membership/app/controller/class-ms-controller-api.php

    Hope this helps

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.