PHP Code Snippet: Check if user has an active subscription

Learn how to check in PHP if a user or customer has an active subscription to a specific membership plan in your WordPress membership site.

To check if a user (with an ID of 50) has an active subscription to a membership plan (with an ID of 3), see the code below.

/**
 * Returns true if user has an active subscription to a plan. False otherwise.
 */
ppress_has_active_subscription(50, 3);

If you want to check by using the customer ID, here’s how:

/**
 * Returns true if the customer has an active subscription to a plan. False otherwise.
 */
ppress_has_active_subscription(50, 3, true);

You can also do a capability check as follows to check if a user is subscribed to a specific membership plan:

/**
 * Returns true if user has an active subscription to a plan. False otherwise.
 */
user_can(50, 'ppress_plan_3');

If you want to check if the currently logged-in user is subscribed to a specific membership plan, see the code snippet below.

/**
 * Returns true if currently logged-in user is subscribed to a memebrship plan. False otherwise.
 */
current_user_can('ppress_plan_3');

La fin!