How to read out the membership level from the database

Hi,

I am using this plugin to managed my members. I know want to do some customization in php based on the membership level. What is the easiest way to read out from the database what membership a wordpress user has. I see that in usermeta, my wordpress site has a new entry called ms_subscriptions for each user. Is that the right field? If yes, how do I identify the current membership level.

Best regards,
Luke

  • Denitsa

    Hello Luke,
    I hope you are having an amazing day!

    Do you require to read from the database? I can offer another, less intrusive solution (it's always best not to touch the database if you can).

    You can get the ID(s) of a member's membership by using the following code snippet.

    $member = MS_Model_Member::get_current_member();
    
    	foreach ( $member->subscriptions as $subscription ) {
    	    $membership = $subscription->get_membership();
    
    	    $membership_id[] = $membership->id;
    	}

    The $membership_id array will contain the ID(s) of the memberships for the current logged in user.

    With the following conditional tag, you can also check if the user is member of membership ID 123.

    $member = MS_Model_Member::get_current_member();
    
    if ( $member->has_membership( 123 ) ) {
        echo "Thanks for subscribing to membership 123";
    }

    I hope this information has been helpful. If I can help you in this or other questions, please let me know!

    Warm regards,
    Denitsa

  • Luke

    Dear Denitsa,

    Thank you for the quick reply. In order to run the above functions, what php files to I need to include? If possible, it would be great to know how to read it directly. How would I read the current membership through the database?

    On another question, I noticed that once a membership expires it does not downgrade automatically to the lower level membership. Is this a configuration problem by me? How can I ensure that once the finite access membership expires, members are automatically downgraded?

    Best regards,
    Luke

  • Sajid

    Hello Luke,
    Hope you are doing good today

    You can add the code in functions.php file of your child theme or use use it as mu-plugin. As well as this code, you can also use any other API functions of Membership 2 Pro plugin. For full reference of the functions and how to use them, please go to Dashboard -> Membership 2 -> Help -> API Docs.

    If you go with mu-plugin route then, go to your /wp-content/ folder via FTP, and find the folder called "mu-plugins". If there is no folder with that name, then you'll want to create one. Then, create a file inside that folder, and give the file any name you like and paste the code in there.

    You don't need to activate that plugin, as it will be always be automatically activated. Please be sure to add a PHP start tag at the beginning of the code if you insert this code as a mu-plugin, like this:

    <?php

    Since Membership 2 Pro plugin save data into multiple DB tables and create a relationship. So I would request you to use API functions instead of reading the data directly. But if you still need to know a way to read data directly then I can ping developer for his valuable feedback on this matter.

    The issue with not dropping members to another membership is a bug, our developers are aware of it and it will be fixed and released as soon as possible.

    Take care and have a nice day

    Best Regards,
    Sajid - WPMU DEV Support