-
-
Save cdsaenz/556623a21969591b28c0865223aa69f9 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment