How to Restrict WordPress REST API via MemberPress

As modern WordPress development moves toward decoupled architectures, mobile applications, and dynamic JavaScript dashboards, developers frequently rely on the WordPress REST API to fetch and manipulate data. However, when running a membership site built with MemberPress, you must ensure that sensitive API endpoints are strictly protected so that only users with active subscriptions can access them.

Instead of relying on third-party security plugins, you can easily secure custom REST API routes using native WordPress permission callbacks combined with MemberPress user subscription checks.

In this tutorial, we will walk through how to protect a custom REST API endpoint based on membership tiers.

1. Registering a Custom REST API Endpoint

To introduce a new API route, we hook into rest_api_init and use register_rest_route(). We also define a custom permission_callback to handle authentication and subscription verification.

Add the following code to your custom plugin or theme’s functions.php:

				
					if (!defined('ABSPATH')) {
    exit;
}

add_action('rest_api_init', 'wg_register_secure_membership_endpoint');

function wg_register_secure_membership_endpoint() {
    register_rest_route('webgarage/v1', '/premium-data/', [
        'methods'             => 'GET',
        'callback'            => 'wg_get_secure_membership_data',
        'permission_callback' => 'wg_verify_subscriber_permission',
    ]);
}
				
			

2. Verifying MemberPress Subscription in the Permission Callback

The permission_callback runs before your main endpoint logic executes. Here, we verify if the user is authenticated via WordPress cookies or application passwords, fetch the MeprUser object, and check if they hold an active subscription to a required product ID (e.g., ID 123).

				
					function wg_verify_subscriber_permission($request) {
    // 1. Check if user is logged in
    if (!is_user_logged_in()) {
        return new WP_Error(
            'rest_forbidden',
            __('You must be logged in to access this resource.', 'webgarage'),
            ['status' => 401]
        );
    }

    $user_id = get_current_user_id();
    $required_product_id = 123; // Replace with your MemberPress Product ID

    // 2. Fetch MemberPress user wrapper
    if (class_exists('MeprUtils')) {
        $mepr_user = new MeprUser($user_id);

        // 3. Check if user has an active subscription/transaction for the product
        if ($mepr_user && $mepr_user->is_active_member_in_product($required_product_id)) {
            return true; // Access granted
        }
    }

    // 4. Reject access if subscription is missing or expired
    return new WP_Error(
        'rest_restricted_membership',
        __('An active membership subscription is required to access this endpoint.', 'webgarage'),
        ['status' => 403]
    );
}
				
			

3. Returning Secure JSON Data from the Endpoint

Once the permission callback validates successfully, your main callback function executes and returns the protected data as a secure JSON response:

				
					function wg_get_secure_membership_data($request) {
    $current_user = wp_get_current_user();

    $data = [
        'success'   => true,
        'message'   => __('Welcome to the VIP member data feed!', 'webgarage'),
        'user'      => [
            'id'    => $current_user->ID,
            'email' => $current_user->user_email,
        ],
        'timestamp' => current_time('mysql'),
    ];

    return rest_ensure_response($data);
}
				
			

Integrating MemberPress subscription checks directly into REST API permission callbacks allows you to build secure, API-driven membership applications, mobile apps, and headless platforms with complete programmatic control.

Be sure to check out our previous tutorials on WordPress development:
How to Programmatically Create WooCommerce Orders
How to Programmatically Create MemberPress Subscriptions
How to Add & Save Custom WooCommerce Checkout Fields

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.

Comments

No comments yet

Share Your Thoughts

Join the conversation!

Scroll to Top

Share Your Thoughts

Thank you for your comment!

Your email address will not be published. Required fields are marked *

Pro

Thank you for reaching out! We’ll be in touch within 24 hours to discuss your project

Standard

Thank you for reaching out! We’ll be in touch within 24 hours to discuss your project

Basic

Thank you for reaching out! We’ll be in touch within 24 hours to discuss your project