While MemberPress provides excellent built-in rules to restrict pages, posts, and categories, advanced applications often require programmatic control. Whether you need to conditionally load features in your custom dashboard, restrict access to specific REST API endpoints based on membership tiers, or redirect users dynamically after login, you need to query MemberPress user data directly via PHP.
In this tutorial, we will explore how to interact with the MemberPress user and subscription classes to check active memberships programmatically.
1. Retrieving the Current MemberPress User Object
MemberPress extends the standard WordPress user with its own robust wrapper class: MeprUser. To fetch the current logged-in user as a MemberPress user object, you use MeprUtils::get_current_mepr_user().
Add the following helper function to your custom plugin or theme’s functions.php:
if (!defined('ABSPATH')) {
exit;
}
function wg_get_current_mepr_user_data() {
if (!is_user_logged_in()) {
return false;
}
$mepr_user = MeprUtils::get_current_mepr_user();
if (!$mepr_user || !is_object($mepr_user)) {
return false;
}
return $mepr_user;
}
2. Checking Active Subscriptions for Specific Products
To check if a user holds an active subscription to a specific membership ID (e.g., ID 123), MemberPress provides built-in methods like is_active_member_in_product().
You can use this inside your custom templates, shortcodes, or plugin logic:
function wg_user_has_active_membership($product_id) {
$mepr_user = wg_get_current_mepr_user_data();
if (!$mepr_user) {
return false;
}
// Returns true if the user has an active subscription/transaction for this product ID
return $mepr_user->is_active_member_in_product($product_id);
}
// Example usage:
// if (wg_user_has_active_membership(123)) { ... }
If you want to fetch all active product IDs the user is currently subscribed to, you can loop through their active subscriptions:
function wg_get_user_active_product_ids() {
$mepr_user = wg_get_current_mepr_user_data();
if (!$mepr_user) {
return [];
}
$subscriptions = $mepr_user->active_subscriptions();
$product_ids = [];
foreach ($subscriptions as $sub) {
// $sub is an instance of MeprSubscription
$product_ids[] = $sub->product_id;
}
return array_unique($product_ids);
}
3. Conditional Redirects Based on Membership Tier
Sometimes you want to redirect users away from certain pages if they don’t hold the required subscription. We can hook into template_redirect and combine it with our check:
add_action('template_redirect', 'wg_restrict_custom_page_by_membership');
function wg_restrict_custom_page_by_membership() {
// Target a specific custom page slug (e.g., /pro-dashboard/)
if (is_page('pro-dashboard')) {
// Define required Membership ID (e.g., Gold Tier = 456)
$required_product_id = 456;
if (!wg_user_has_active_membership($required_product_id)) {
// Redirect to pricing page or account page safely
wp_safe_redirect(home_url('/pricing/'));
exit;
}
}
}
Using MemberPress programmatic methods like is_active_member_in_product() and active_subscriptions() gives you full flexibility to build advanced conditional logic, custom dashboards, and tailored user flows.
Be sure to check out our previous tutorials on WordPress development:
How to Add & Process Custom Fields in MemberPress
How to Add Custom User Meta & Profile Fields
How to Handle Nonces & Form Security in WordPress
Need help with your project? We will be happy to help with your project. Feel free to contact us. Just reach out and we will reply soon.