How to Handle MemberPress Events & Webhooks in PHP

MemberPress provides an event-driven architecture that allows developers to run custom PHP code whenever a membership state changes. Whether a customer purchases a new plan, renews their subscription, upgrades to a higher tier, or cancels their account, MemberPress triggers internal event hooks.

By listening to these events, you can integrate MemberPress with external CRMs, update custom database records, send automated notifications, or manage API permissions seamlessly.

In this tutorial, we will explore how to hook into MemberPress events using mepr-event-* actions.

1. Understanding MemberPress Event Structure

Whenever an event occurs, MemberPress creates an instance of MeprEvent. The event object holds details about the action (e.g., subscription-created, transaction-completed, subscription-stopped) and contains the associated data object (such as MeprTransaction, MeprSubscription, or MeprUser).

You can listen to specific events or hook into all events dynamically using the mepr-event-{$event_name} action.

2. Handling Subscription Creation Events

To perform an action as soon as a user successfully signs up for a recurring subscription, hook into mepr-event-subscription-created.

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

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

add_action('mepr-event-subscription-created', 'wg_mepr_on_subscription_created');

function wg_mepr_on_subscription_created($event) {
    // Extract the subscription object from the event
    $subscription = $event->get_data();

    if (!$subscription instanceof MeprSubscription) {
        return;
    }

    $user_id    = $subscription->user_id;
    $product_id = $subscription->product_id;
    $user       = get_userdata($user_id);

    // Example action: Sync user with an external CRM or API
    wg_sync_user_to_external_crm($user->user_email, $product_id);
}

function wg_sync_user_to_external_crm($email, $membership_id) {
    // Custom API integration logic here
}
				
			

3. Handling Subscription Cancellations & Expirations

When a user cancels their recurring billing or their membership expires, you often need to revoke external privileges or update internal user statuses.

You can listen to mepr-event-subscription-stopped or mepr-event-transaction-expired:

				
					add_action('mepr-event-subscription-stopped', 'wg_mepr_on_subscription_cancelled');

function wg_mepr_on_subscription_cancelled($event) {
    $subscription = $event->get_data();

    if ($subscription instanceof MeprSubscription) {
        $user_id    = $subscription->user_id;
        $product_id = $subscription->product_id;

        // Example action: Remove custom user meta or update status in CRM
        update_user_meta($user_id, 'wg_crm_sync_status', 'cancelled');
    }
}
				
			

4. Catching All MemberPress Events Dynamically

If you want to log all membership events or build a central webhook dispatcher, you can listen to the generic mepr-event hook:

				
					add_action('mepr-event', 'wg_log_all_mepr_events');

function wg_log_all_mepr_events($event) {
    $event_name = $event->event; // e.g. 'transaction-completed'
    $event_data = $event->get_data();

    // Log event details for debugging or auditing
    error_log(sprintf('MemberPress Event Triggered: %s for Event ID %d', $event_name, $event->id));
}
				
			

Using MemberPress event hooks (mepr-event-*) allows you to extend subscription workflows cleanly, ensuring your custom integrations stay in sync with payment lifecycle events without modifying core files.

Be sure to check out our previous tutorials on WordPress development:
How to Check MemberPress Subscriptions & Redirect Users
How to Add & Process Custom Fields in MemberPress
How to Add Custom User Meta & Profile 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