Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ipokkel/8e9af69fc43e75376e81b5879650eaf2 to your computer and use it in GitHub Desktop.
Save ipokkel/8e9af69fc43e75376e81b5879650eaf2 to your computer and use it in GitHub Desktop.
Add PMPro Billing Address fields to the user profile edit pages.
<?php
/**
* This will add billing fields to the administrative user profile edit page for administrators
* and on the frontend PMPro user Edit Profile page for members.
*
* This code recipe requires Paid Memberships Pro version 2.9.0 or higher.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function add_billing_fields_to_user_profile_edit() {
// Don't break if PMPro is out of date or not loaded.
if ( ! function_exists( 'pmpro_add_user_field' ) ) {
return false;
}
global $pmpro_countries;
// Define the fields
$fields = array();
// List the fields you want to include
$address_fields = array(
'pmpro_bfirstname' => 'First Name',
'pmpro_blastname' => 'Last Name',
'pmpro_baddress1' => 'Address 1',
'pmpro_baddress2' => 'Address 2',
'pmpro_bcity' => 'City',
'pmpro_bstate' => 'State',
'pmpro_bzipcode' => 'Zipcode',
'pmpro_bcountry' => 'Country',
'pmpro_bphone' => 'Phone',
);
foreach ( $address_fields as $name => $label ) {
$options = 'pmpro_bcountry' === $name ? $pmpro_countries : array();
$type = 'pmpro_bcountry' === $name ? 'select' : 'text';
// Set all fields required except Address 2.
$required = 'pmpro_baddress2' !== $name ? true : false;
$html_attributes = 'pmpro_baddress2' !== $name ? array( 'required' => 'required' ) : '';
$fields[] = new PMPro_Field(
$name,
$type,
array(
'label' => $label,
'size' => 40,
'profile' => 'only',
'options' => $options,
'addmember' => true,
'required' => $required, // comment this out to make the fields optional.
'html_attributes' => $html_attributes, // comment this out to make the fields optional.
)
);
}
// Add new checkout box with label
pmpro_add_field_group( 'billing_address_profile', 'Billing Address' );
// Add fields into a new checkout_boxes area of checkout page
foreach ( $fields as $field ) {
pmpro_add_user_field(
'billing_address_profile', // location on checkout page
$field // PMPro_Field object
);
}
}
add_action( 'init', 'add_billing_fields_to_user_profile_edit' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment