Skip to content

Instantly share code, notes, and snippets.

@cartpauj
Created February 5, 2016 18:58
Show Gist options
  • Save cartpauj/63451b7e97f2c9cb4ebf to your computer and use it in GitHub Desktop.
Save cartpauj/63451b7e97f2c9cb4ebf to your computer and use it in GitHub Desktop.
Collect a website URL from users at signup in MemberPress. Allows you to store a different website URL with each user's subscription.
<?php
/*
Plugin Name: MemberPress Website Fields
Version: 1.0.0
Author: Caseproof, LLC
Author URI: http://caseproof.com
Description: Allow users to enter their website URL for each subscription they purchase
*/
//Signup form functions
function mepr_show_signup_fields() {
?>
<div class="mp-form-row mepr_website_field">
<div class="mp-form-label">
<label>Website:*</label>
</div>
<input type="text" name="mepr_website_field" id="mepr_website_field" class="mepr-form-input" value="<?php echo (isset($_POST['mepr_website_field']))?stripslashes($_POST['mepr_website_field']):''; ?>" />
</div>
<?php
}
function mepr_validate_fields($errors, $user = null) {
if(!isset($_POST['mepr_website_field']) || empty($_POST['mepr_website_field'])) {
$errors[] = "You must enter a website URL";
}
return $errors;
}
function mepr_save_signup_fields($amount, $user, $prod_id, $txn_id) {
if(!isset($_POST['mepr_website_field']) || empty($_POST['mepr_website_field'])) {
return; //Nothing to do bro!
}
$txn = new MeprTransaction($txn_id);
$sub = $txn->subscription();
$website_fields = get_user_meta($user->ID, 'mepr_custom_website_fields', true);
if(!$website_fields) {
$website_fields = array();
}
if($sub !== false && $sub instanceof MeprSubscription) {
$website_fields[] = array('txn_id' => $txn_id,
'recurring' => true,
'sub_id' => $sub->ID,
'website' => stripslashes($_POST['mepr_website_field'])
);
}
else {
$website_fields[] = array('txn_id' => $txn_id,
'recurring' => false,
'sub_id' => 0,
'website' => stripslashes($_POST['mepr_website_field'])
);
}
update_user_meta($user->ID, 'mepr_custom_website_fields', $website_fields);
}
//Signup form hooks
add_action('mepr-before-coupon-field', 'mepr_show_signup_fields');
add_filter('mepr-validate-signup', 'mepr_validate_fields');
add_action('mepr-process-signup', 'mepr_save_signup_fields', 10, 4);
//Account Subscriptions table Functions
function mepr_add_subscriptions_th($user, $subs) {
?>
<th>Site</th>
<?php
}
function mepr_add_subscriptions_td($user, $sub, $txn, $is_recurring) {
$website = 'None';
$website_fields = get_user_meta($user->ID, 'mepr_custom_website_fields', true);
if($website_fields) {
foreach($website_fields as $f) {
if($is_recurring && $sub->ID == $f['sub_id']) {
$website = $f['website'];
break;
}
elseif(!$is_recurring && $txn->id == $f['txn_id']) {
$website = $f['website'];
break;
}
}
}
?>
<td data-label="Website">
<div class="mepr-account-website"><?php echo $website; ?></div>
</td>
<?php
}
//Account Subscriptions table hooks
add_action('mepr-account-subscriptions-th', 'mepr_add_subscriptions_th', 10, 2);
add_action('mepr-account-subscriptions-td', 'mepr_add_subscriptions_td', 10, 4);
//Admin Subscriptions table functions
function mepr_add_admin_subscriptions_cols($cols, $prefix, $lifetime) {
$cols[$prefix.'site'] = 'Site';
return $cols;
}
//NOT NEEDED
// function mepr_add_admin_subscriptions_sortable_cols($cols, $prefix, $lifetime) {
// $cols[$prefix.'site'] = false;
// return $cols;
// }
function mepr_add_admin_subscriptions_cell($column_name, $rec, $table, $attributes) {
$user = new MeprUser($rec->user_id);
if(strpos($column_name, '_site') !== false && (int)$user->ID > 0) {
$website = 'None';
$website_fields = get_user_meta($user->ID, 'mepr_custom_website_fields', true);
if($website_fields) {
foreach($website_fields as $f) {
if(!$table->lifetime && $rec->ID == $f['sub_id']) {
$website = $f['website'];
break;
}
elseif($table->lifetime && $rec->ID == $f['txn_id']) {
$website = $f['website'];
break;
}
}
}
?>
<td <?php echo $attributes; ?>><?php echo $website; ?></td>
<?php
}
}
//Admin Subscriptions table hooks
add_filter('mepr-admin-subscriptions-cols', 'mepr_add_admin_subscriptions_cols', 10, 3);
// add_filter('mepr-admin-subscriptions-sortable-cols', 'mepr_add_admin_subscriptions_sortable_cols', 10, 3); //NOT NEEDED
add_action('mepr-admin-subscriptions-cell', 'mepr_add_admin_subscriptions_cell', 10, 4);
@Overci
Copy link

Overci commented Jun 15, 2019

Howdy, Paul. Thanks so much for all your MemberPress snippets and code. Is this item still compatible with the current version of MemberPress? I have installed this website-fields.php as a plugin, and it runs without errors, but the additional field is not displayed on the admin subscriptions (tested for one-time / lifetime only). The "Site" column is there, but it always shows "None." However, the custom fields are successfully collected and stored, as I can see them from the member's account subscriptions page (when logged in as member). Everything seems to be working great except for the admin display on the subscriptions table. Any ideas?

@cartpauj
Copy link
Author

It's been a while since I wrote this. I think this only works on recurring subscriptions. Are you doing automatically recurring payments, or just one-time payments?

@Overci
Copy link

Overci commented Jun 17, 2019

Thanks for the response, Paul. I actually managed to get it fixed late last night (or at least it seems to be fixed). On line 126 of the original code above, I changed "txn_id" to "sub_id" and that did the trick. I am guessing because for lifetime memberships (all my stuff is just one-time payments, thus lifetime memberships), there is no lifetime transaction, but there is a lifetime subscription. Just a guess though; I did not dig into it more deeply. Anyway, that change on line 126 seems to have done the trick for my site. Thanks again for all your help!

@slackday
Copy link

slackday commented Feb 1, 2022

Oh great the completely undocumented mepr-process-signup 👀. Glad I found this gist and thanks so much for putting all this documentation together! 👍

Do you still work with MemberPress. I'm trying to create a custom payment cycle where I need to upgrade memberships and set next payment due date depending on if they pay first or second half of the year. Any idea what the best hook is to use?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment