Membership IDs and the has_membership method

Two questions tied to membership IDs:
1) How do I use the has_membership method?
2) Where can I find the IDs for the different membership types?

Thanks!

  • Adam Czajczyk

    Hello Mark,

    I hope you're well today!

    1) How do I use the has_membership method?

    You can use it inside your theme files (e.g page templates) and/or plugins to verify user against selected membership. Let's say you're building a custom page template and would like to display content to the members of "Gold" membership only.

    First step is to find an ID of "Gold" membership (see below). Second is to add "has_membership()" to the template code. Assuming you'd normally display content like this:

    <div class='entry'>
    <?php the_content(); ?>
    </div>

    you may use this code to include membership check (replace XXX with membership ID):

    <div class='entry'>
    <?php
    if (has_membership('XXX')) {
       the_content();
    }
    else {
      ?>
      This content is restricted;
      <?php
    }
    ?>
    </div>

    2) Where can I find the IDs for the different membership types?

    The "membership ID" is the same ID that's used for protection shortcodes. Therefore you'll find those on you dashboard's

    "Membership 2" -> "Membership" page (right column).

    Next to each membership there's a shortcode listed including its ID. You may use the same ID for "has_membership()".

    I hope that helps!

    Cheers,
    Adam

  • Mark Blevis

    I'm trying to display custom profile fields (user meta) ONLY if the user being updated has certain membership.

    If I cast the user as a Member, has_memberships always returns true
    If I just use the user as is, has_memberships always returns false.

    Here is the code:

    add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
    add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );

    /* If user has BranchAdmin membership, also gather or display associated branch: */
    function my_show_extra_profile_fields( $user ) {
    $member = new MS_Model_Member( $user );
    if (($member->has_membership('15692')) || /* BranchAdmin membership */
    ($member->has_membership('15691')) || /* BranchMember membership */
    ($member->has_membership('15690')) ) /* Corps Member membership */ { ?> ALWAYS TRUE

    - OR -
    * If user has BranchAdmin membership, also gather or display associated branch: */
    function my_show_extra_profile_fields( $user ) {
    if (($user->has_membership('15692')) || /* BranchAdmin membership */
    ($user->has_membership('15691')) || /* BranchMember membership */
    ($usre->has_membership('15690')) ) /* Corps Member membership */ { ?> ALWAYS FALSE

    Thanks for any help you can provide.

  • Adam Czajczyk

    Hey Mark!

    Thanks for sharing your code with me.

    There are few things worth changing here:

    1. $user is an object here and since we need a user ID only, this is the way to get it "$user->ID";

    2. to use "has_membership()" this way you first need to access Membership 2 api and the simplest way is "$api = ms_api()"

    3. There's a typo in last line of your code ($usre instead of $user) :slight_smile:

    That said, take a look at this code please:

    add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
    add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
    
    /* If user has BranchAdmin membership, also gather or display associated branch: */
    function my_show_extra_profile_fields( $user ) {
    $api = ms_api();
    $member = $api -> get_member($user->ID);
    if (($member->has_membership('15692')) || /* BranchAdmin membership */
    ($member->has_membership('15691')) || /* BranchMember membership */
    ($member->has_membership('15690')) ) /* Corps Member membership */ { ?> 
    
    is member
    
    <?php }
    
    else {
    	?>
    	not a member
    	<?
    }
    }

    This seems to be working as expected on my test setup so hopefully will work for you as well. Please note also that this code will only work for users viewing their own profiles.

    I hope that helps!

    Cheers,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.