Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Last active August 19, 2022 06:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipokkel/99df1c93e7f2082a4c64b9c25549b590 to your computer and use it in GitHub Desktop.
Save ipokkel/99df1c93e7f2082a4c64b9c25549b590 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.
*
* 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() {
// Require PMPro and PMPro Register Helper
if ( ! defined( 'PMPRO_VERSION' ) || ! function_exists( 'pmprorh_add_registration_field' ) ) {
return;
}
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';
$fields[] = new PMProRH_Field(
$name,
$type,
array(
'label' => $label,
'size' => 40,
'profile' => 'only',
'options' => $options,
'addmember' => true,
'required' => true, // comment this out to make the fields optional.
'html_attributes' => array( 'required' => 'required' ), // comment this out to make the fields optional.
)
);
}
// Add new checkout box with label
pmprorh_add_checkout_box( 'billing_address_profile', 'Billing Address' );
// Add fields into a new checkout_boxes area of checkout page
foreach ( $fields as $field ) {
pmprorh_add_registration_field(
'billing_address_profile', // location on checkout page
$field // PMProRH_Field object
);
}
}
add_action( 'init', 'add_billing_fields_to_user_profile_edit' );
@ipokkel
Copy link
Author

ipokkel commented Aug 19, 2022

For PMPro version 2.9.0 or higher use https://gist.github.com/ipokkel/8e9af69fc43e75376e81b5879650eaf2 (does not require Register Helper)

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