public
Last active

Paid Memberships Pro Extra Checkout/Profile Fields Example

  • Download Gist
gistfile1.aw
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
<?php
/*
Adding First and Last Name to Checkout Form
*/
 
//add the fields to the form
function my_pmpro_checkout_after_password()
{
if(!empty($_REQUEST['firstname']))
$firstname = $_REQUEST['firstname'];
else
$firstname = "";
if(!empty($_REQUEST['lastname']))
$lastname = $_REQUEST['lastname'];
else
$lastname = "";
if(!empty($_REQUEST['companyname']))
$companyname = $_REQUEST['companyname'];
else
$companyname = "";
if(!empty($_REQUEST['repname']))
$repname = $_REQUEST['repname'];
else
$repname = "";
?>
<div>
<label for="firstname">First Name</label>
<input id="firstname" name="firstname" type="text" class="input" size="30" value="<?=esc_attr($firstname)?>" />
</div>
<div>
<label for="lastname">Last Name</label>
<input id="lastname" name="lastname" type="text" class="input" size="30" value="<?=esc_attr($lastname)?>" />
</div>
<div>
<label for="companyname">Company Name</label>
<input id="companyname" name="companyname" type="text" class="input" size="30" value="<?=esc_attr($companyname)?>" />
</div>
<div>
<label for="repname">Rep Number/Name</label>
<input id="repname" name="repname" type="text" class="input" size="30" value="<?=esc_attr($repname)?>" />
</div>
<?php
}
add_action('pmpro_checkout_after_password', 'my_pmpro_checkout_after_password');
 
//update the user after checkout
function my_update_first_and_last_name_after_checkout($user_id)
{
if(isset($_REQUEST['firstname']))
{
$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
$companyname = $_REQUEST['companyname'];
$repname = $_REQUEST['repname'];
}
elseif(isset($_SESSION['firstname']))
{
//maybe in sessions?
$firstname = $_SESSION['firstname'];
$lastname = $_SESSION['lastname'];
$companyname = $_SESSION['companyname'];
$repname = $_SESSION['repname'];
//unset
unset($_SESSION['firstname']);
unset($_SESSION['lastname']);
unset($_SESSION['companyname']);
unset($_SESSION['repname']);
}
if(isset($firstname))
update_user_meta($user_id, "first_name", $firstname);
if(isset($lastname))
update_user_meta($user_id, "last_name", $lastname);
if(isset($companyname))
update_user_meta($user_id, "company_name", $companyname);
if(isset($repname))
update_user_meta($user_id, "rep_name", $repname);
}
add_action('pmpro_after_checkout', 'my_update_first_and_last_name_after_checkout');
 
//require the fields
function my_pmpro_registration_checks()
{
global $pmpro_msg, $pmpro_msgt, $current_user;
$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
$companyname = $_REQUEST['companyname'];
$repname = $_REQUEST['repname'];
if($firstname && $lastname && $companyname && $repname || $current_user->ID)
{
//all good
return true;
}
else
{
$pmpro_msg = "The first name, last name, company name, and rep number/name fields are required.";
$pmpro_msgt = "pmpro_error";
return false;
}
}
add_filter("pmpro_registration_checks", "my_pmpro_registration_checks");
 
function my_show_extra_profile_fields($user)
{
?>
<h3>Extra profile information</h3>
 
<table class="form-table">
 
<tr>
<th><label for="companyname">Company Name</label></th>
 
<td>
<input type="text" name="companyname" id="companyname" value="<?php echo esc_attr( get_user_meta($user->ID, 'company_name', true) ); ?>" class="regular-text" /><br />
</td>
</tr>
<tr>
<th><label for="repname">Rep Number/Name</label></th>
 
<td>
<input type="text" name="repname" id="repname" value="<?php echo esc_attr( get_user_meta($user->ID, 'rep_name', true) ); ?>" class="regular-text" /><br />
</td>
</tr>
 
</table>
<?php
}
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
 
 
function my_save_extra_profile_fields( $user_id )
{
 
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
 
if(isset($_POST['companyname']))
update_usermeta( $user_id, 'company_name', $_POST['companyname'] );
if(isset($_POST['repname']))
update_usermeta( $user_id, 'rep_name', $_POST['repname'] );
}
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
 
/*
These bits are required for PayPal Express only.
*/
function my_pmpro_paypalexpress_session_vars()
{
//save our added fields in session while the user goes off to PayPal
$_SESSION['firstname'] = $_REQUEST['firstname'];
$_SESSION['lastname'] = $_REQUEST['lastname'];
$_SESSION['companyname'] = $_REQUEST['companyname'];
$_SESSION['repname'] = $_REQUEST['repname'];
}
add_action("pmpro_paypalexpress_session_vars", "my_pmpro_paypalexpress_session_vars");
?>

where to add this ?

SO where do we add this to? Thanks

Could you give an example to add also add a drop down in addition to the text input fields?

thanks!

BTW since a few asked, this goes in functions.php

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.