Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Created November 23, 2011 21:17
Show Gist options
  • Save strangerstudios/1389944 to your computer and use it in GitHub Desktop.
Save strangerstudios/1389944 to your computer and use it in GitHub Desktop.
Paid Memberships Pro Extra Checkout/Profile Fields Example
<?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");
?>
@virtualgeorge
Copy link

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

@bestslopedesigns
Copy link

Or you can add it to a custom plugin, which will survive your theme's updates. See Jason's article, here: [https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/]

@Meseeksivs
Copy link

Is there any way to add the new data to the admin confirmation mail?

@ideadude
Copy link

ideadude commented Oct 9, 2019

I cannot edit this file anymore to improve it, but it needs sanitization and escaping to make it secure.

A better way to do stuff like this is with the PMPro Register Helper Add On. Docs here: https://www.paidmembershipspro.com/add-ons/pmpro-register-helper-add-checkout-and-profile-fields/

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