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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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!'; | |
} |
You can use this kind of code everywhere, within a hook or inside a template, a widget, a shortcode. Wherever you want.
Leave a Reply