Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipokkel/47a8de3553e84bfe08d079d2952c8486 to your computer and use it in GitHub Desktop.
Save ipokkel/47a8de3553e84bfe08d079d2952c8486 to your computer and use it in GitHub Desktop.
Create user fields with code to add Paid Memberships Pro Billing Address as required fields to Edit Profile, User Profile Edit (admin), and Add Member from Admin pages. #paid-memberships-pro
<?php
/**
* This will add billing fields to Add Member from Admin and the user profile edit pages.
*
* Optional: Set if fields should be required
*
* Requires Paid Memberships Pro and PMPro Register Helper plugins active.
*
* 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_pmpro_billing_fields_to_edit_profile_and_add_member() {
#-- SETTINGS ---#
/* Should the fields be required? */
$required = true; // (Boolean) true: fields are required | false: fields are optional.
#-- END SETTINGS ---#
/* That's it, no further editing required */
// Require PMPro
if ( ! function_exists( 'pmpro_hasMembershipLevel' ) ) {
return;
}
// Get countries.
global $pmpro_countries;
// Create an array for the fields.
$fields = array();
// Define the fields
// Add a Billing Address title for Add Member from Admin page.
if ( is_admin() && isset( $_REQUEST['page'] ) && 'pmpro-addmember' == $_REQUEST['page'] ) {
$fields[] = new PMPro_Field(
'addmember_billing_title',
'html',
array(
'label' => '<strong>Billing Address</strong>',
'profile' => 'only_admin',
'addmember' => true,
)
);
}
// 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',
);
// Build the fields.
foreach ( $address_fields as $name => $label ) {
// Set field options for country field.
$options = 'pmpro_bcountry' === $name ? $pmpro_countries : array();
$type = 'pmpro_bcountry' === $name ? 'select' : 'text';
// Should the field be required?
$required_option = 'pmpro_baddress2' !== $name ? $required : false; // Address 2 is always optional.
// Add HTML required attribute to check field before submit.
$required_html = $required_option ? array( 'required' => 'required' ) : array();
$fields[] = new PMPro_Field(
$name,
$type,
array(
'label' => $label,
'size' => 40,
'profile' => 'only',
'options' => $options,
'addmember' => true,
'required' => $required_option,
'html_attributes' => $required_html,
)
);
}
// Add new checkout box with label.
pmpro_add_field_group( 'billing_address_profile', 'Billing Address' );
// Add fields into the custom checkout boxes area created.
foreach ( $fields as $field ) {
pmpro_add_user_field(
'billing_address_profile', // location on checkout page
$field // PMProRH_Field object
);
}
}
add_action( 'init', 'add_pmpro_billing_fields_to_edit_profile_and_add_member' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment