Check if a user is an active member of a membership plan

Some weeks ago we released the brand new WooCommerce Memberships plugin.

From the plugin description:

Memberships allows you to create an entire membership system without compiling a Frankenstein assortment of plugins.

That’s true, with WooCommerce Memberships you can create membership plans, manage members and other membership stuff related very easily.

It still does not support every possible integration with other plugins, but some of them are supported.

In some cases though, you would need to check whether or not a user is an actual member of one of your plans.

WooCommerce Memberships has some functions you can use to do that via custom code, there’s a complete list here.

I want to show you how to use one of these functions, wc_memberships_is_user_active_member.

How to check if a user is an active member of a plan?

The function wc_memberships_is_user_active_member accepts two parameters, the first is the user ID and the second is the plan slug (post object or related post ID).

Using the plan slug is easier, I’ll use that for the example.

Let’s say that you have a two different plans for a membership, Silver and Gold.

You have something in the footer, a very special message, and you want to show it only to the members of the plan Gold.

You would use a code like this:


// Get current user ID
$user_id = get_current_user_id();
// Check if the user is member of the plan 'gold'
if ( wc_memberships_is_user_active_member( $user_id, 'gold' ) ) {
echo 'This is a special message only for Gold members!';
} else {
echo 'I\'m sorry, you are not a gold member. There\'s nothing here for you!';
}

view raw

functions.php

hosted with ❤ by GitHub

You can use this kind of code everywhere, within a hook or inside a template, a widget, a shortcode. Wherever you want.


More Posts That You Might Like…


7 responses to “Check if a user is an active member of a membership plan”

  1. Thank you ,, it helped so much

  2. Thanks!
    Is that possible to repace the slug with a variable?
    For exemple, I want to put that code on content-single-product.php and check if the plan is linked to that product.

    1. Hi Nicolas,
      Yes sure you can replace the slug with a variable.

  3. Hi is there a way to check if membership free trail ended for a specific user?

    1. Hey John,
      Looks like you are using it with WooCommerce Subscriptions to handle the free trial?

      You need to check the trial expiration on the subscription, not the membership. You can do that with the functions provided by WooCommerce Subscriptions.

      You can find the developers documentation here: https://docs.woocommerce.com/documentation/plugins/woocommerce/woocommerce-extensions/woocommerce-subscriptions/developer-docs/

      1. Thank you for your help.

  4. Hi Nicolas, I’m trying to use this function to show only comments from people who are members of the same group. Do you have any idea how I could get this right?

    add_filter( ‘comments_array’ , ‘members_filter’ , 10, 2 );
    function members_filter( $comments, $post_id ){

    $current_user = wp_get_current_user();
    foreach( $comments as $key => $comment ){
    $comment_author = new WP_User( $comment->user_id );

    $userid=get_current_user_id();

    if (wc_memberships_is_user_active_member( $userid, 'test_1' )) {
    unset( $comments[$key] );
    }

    }

    return $comments;

    }

Leave a Reply

Categories

Newsletter

Receive new articles from this blog directly in your inbox!

No spam guaranteed!

Blog at WordPress.com.

%d bloggers like this: