While MemberPress handles standard signups and checkouts automatically, advanced projects often require programmatic control. Whether you are building a custom payment gateway integration, importing users and memberships from an external legacy database, or granting access automatically based on an external API webhook, you need to know how to create transactions and subscriptions directly via PHP.
Instead of relying on database hacks, MemberPress provides built-in data classes (MeprTransaction and MeprSubscription) that handle data validation, expiration dates, and user access securely.
In this tutorial, we will walk through how to create and assign a MemberPress membership programmatically.
1. Granting Access by Creating a Programmatic Transaction
For one-time purchases or lifetime access, MemberPress relies primarily on transactions (MeprTransaction). When you create a complete transaction, MemberPress automatically grants the user access to the corresponding product.
Add the following helper function to your custom plugin or theme’s functions.php:
if (!defined('ABSPATH')) {
exit;
}
function wg_mepr_grant_manual_membership($user_id, $product_id, $amount = 0.00, $expiration_date = '0000-00-00 00:00:00') {
// Ensure the user and product exist
if (!get_userdata($user_id) || !get_post($product_id)) {
return false;
}
$txn = new MeprTransaction();
$txn->user_id = $user_id;
$txn->product_id = $product_id;
$txn->amount = $amount;
$txn->total = $amount;
$txn->status = MeprTransaction::$complete_str; // Marks transaction as successful
$txn->gateway = 'manual'; // Or your custom gateway slug
$txn->created_at = current_time('mysql', true);
// Set expiration date ('0000-00-00 00:00:00' for lifetime access, or 'YYYY-MM-DD HH:MM:SS' for limited)
$txn->expires_at = $expiration_date;
// Save the transaction and trigger MemberPress access updates
if ($txn->store()) {
// Optional: Send welcome email or trigger actions
return $txn->id;
}
return false;
}
2. Creating a Recurring Subscription Programmatically
If your workflow involves recurring billing handled by an external script or custom API, you need to create both a subscription (MeprSubscription) and link subsequent transactions to it.
function wg_mepr_create_recurring_subscription($user_id, $product_id, $sub_amount, $sub_period, $sub_period_type) {
if (!get_userdata($user_id)) {
return false;
}
$sub = new MeprSubscription();
$sub->user_id = $user_id;
$sub->product_id = $product_id;
$sub->sub_scr_id = 'wg_sub_' . uniqid(); // Unique gateway subscription ID
$sub->price = $sub_amount;
$sub->total = $sub_amount;
$sub->period = $sub_period; // e.g., 1, 3, 12
$sub->period_type = $sub_period_type; // e.g., 'months', 'years', 'days'
$sub->status = MeprSubscription::$active_str;
$sub->gateway = 'manual';
$sub->created_at = current_time('mysql', true);
$sub->next_billing_at = date('Y-m-d H:i:s', strtotime("+{$sub->period} {$sub->period_type}"));
if ($sub->store()) {
return $sub->id;
}
return false;
}
3. Revoking Membership Programmatically
If you need to cancel or expire a user’s membership programmatically (for instance, when handling a refund webhook from an external system), you can update the transaction or subscription status directly:
function wg_mepr_revoke_user_membership($transaction_id) {
$txn = new MeprTransaction($transaction_id);
if (isset($txn->ID) && $txn->ID > 0) {
// Mark transaction as refunded or expired
$txn->status = MeprTransaction::$refunded_str; // or MeprTransaction::$expired_str
$txn->store();
// This automatically recalculates and revokes user access privileges in MemberPress
return true;
}
return false;
}
Using MemberPress data classes (MeprTransaction and MeprSubscription) allows you to build custom synchronization tools, payment gateways, and automated migration scripts while letting MemberPress handle access control securely.
Be sure to check out our previous tutorials on WordPress development:
How to Add & Save Custom WooCommerce Checkout Fields
How to Add Custom Tabs to the MemberPress Account
How to Modify MemberPress Product Prices Dynamically
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.