While MemberPress provides a robust out-of-the-box membership and subscription system, advanced projects often require capturing additional data during registration—such as a user’s company type, referral source, or custom agreement checkboxes—directly on the checkout form.
Although MemberPress has a built-in custom fields builder, developers frequently need more programmatic control over rendering, backend validation, and saving data securely to user meta.
In this tutorial, we will walk through how to inject custom fields into the MemberPress checkout form using native action hooks and save them upon subscription completion.
1. Injecting Custom Fields into the Checkout Form
MemberPress provides specialized action hooks directly inside the checkout form. We can use mepr-checkout-before-submit to render custom HTML input fields right before the payment/submit button.
Add the following code to your custom plugin or theme’s functions.php:
if (!defined('ABSPATH')) {
exit;
}
// Render custom field on MemberPress checkout
add_action('mepr-checkout-before-submit', 'wg_mepr_add_custom_checkout_field');
function wg_mepr_add_custom_checkout_field($membership_id) {
// Optional: Target a specific membership ID if needed
// if ($membership_id != 123) return;
$value = isset($_POST['wg_company_industry']) ? sanitize_text_field($_POST['wg_company_industry']) : '';
?>
2. Validating Custom Field Input
Before processing the transaction, you must validate the incoming data to ensure it isn’t empty or malicious. MemberPress provides the mepr-validate-signup filter to catch errors before checkout submission.
add_filter('mepr-validate-signup', 'wg_mepr_validate_custom_field', 10, 3);
function wg_mepr_validate_custom_field($errors, $user, $product, $mepr_options) {
if (empty($_POST['wg_company_industry'])) {
$errors[] = __('Please specify your company industry.', 'webgarage');
}
return $errors;
}
3. Saving Custom Data to User Meta
Once the checkout is successfully processed and the transaction is completed, we can capture the user ID and save our custom field data into the WordPress user meta table using mepr-txn-status-complete or mepr-process-signup.
add_action('mepr-txn-status-complete', 'wg_mepr_save_custom_checkout_data');
function wg_mepr_save_custom_checkout_data($transaction) {
// Ensure we have a valid user ID from the transaction
$user_id = $transaction->user_id;
if ($user_id && isset($_POST['wg_company_industry'])) {
$industry = sanitize_text_field($_POST['wg_company_industry']);
update_user_meta($user_id, 'wg_company_industry', $industry);
}
}
Using MemberPress action and filter hooks allows you to seamlessly extend checkout workflows, collect targeted user data, and store custom metadata without hacking core plugin files.
Be sure to check out our previous tutorials on WordPress development:
How to Add Custom User Meta & Profile Fields
How to Handle Nonces & Form Security in WordPress
How to Register Custom Post Types & Taxonomies
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.