Skip to content

Instantly share code, notes, and snippets.

@ronalfy
Last active September 21, 2020 13:42
Show Gist options
  • Save ronalfy/a9ed8c9c693168a8c7c39aafac5551cc to your computer and use it in GitHub Desktop.
Save ronalfy/a9ed8c9c693168a8c7c39aafac5551cc to your computer and use it in GitHub Desktop.
PMPro - Add Company - Remove Required Fields - Change Default Country
<?php
/**
* Adds a Register Helper item.
* Only allows certain billing fields to be required.
* Sets default country selected to Japan
*
* 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/
*/
/**
* Add Company name to checkout.
*/
function pmpro_custom_my_pmprorh_init() {
// Don't break if Register Helper is not loaded.
if ( ! function_exists( 'pmprorh_add_registration_field' ) ) {
return false;
}
// Define the fields.
$fields = array();
$fields[] = new PMProRH_Field(
'company', // input name, will also be used as meta key
'text', // type of field
array(
'label' => 'Société', // custom field label
'size' => 40, // input size
'class' => 'company', // custom class
'profile' => true, // show in user profile
'required' => true, // make this field required
'levels' => array( 1 ), // only levels 1 and 2 should have the company field
)
);
// Add the fields into a new checkout_boxes are of the checkout page.
foreach ( $fields as $field ) {
pmprorh_add_registration_field(
'after_username', // location on checkout page
$field // PMProRH_Field object
);
}
// That's it. See the PMPro Register Helper readme for more information and examples.
}
add_action( 'init', 'pmpro_custom_my_pmprorh_init' );
/**
* remove required billing fields except firstname/lastname/country
*/
function pmpro_custom_remove_billing_fields_required( $required_fields ) {
return array(
'bfirstname' => '',
'blastname' => '',
'bcountry' => '',
);
}
add_filter( 'pmpro_required_billing_fields', 'pmpro_custom_remove_billing_fields_required' );
/**
* Make country Japan by default.
*/
function pmpro_filter_pmpro_default_country( $default_country ) {
$default_country = 'JP';
return $default_country;
}
add_filter( 'pmpro_default_country', 'pmpro_filter_pmpro_default_country' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment