Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Created December 17, 2020 09:36
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/143b8b15ccaf4f857f5e36015527c632 to your computer and use it in GitHub Desktop.
Save ipokkel/143b8b15ccaf4f857f5e36015527c632 to your computer and use it in GitHub Desktop.
Examples of how to create custom Register Helper checkout box locations, add a description to be deisplayed below the title, and set the order in which these checkout boxes are arranged, and create custom registration fields that are published in these custom checkout boxes on the membership registration form. #pmpo-register-helper #location
<?php
/**
* This recipe is an example of how to create custom Register Helper checkout boxes,
* add a description to be displayed below the title, and
* set the order in which these checkout boxes are arranged,
* and create custom registration fields that are published in these custom checkout boxes
* on the membership registration form.
*
* @requires Register Helper Add On
* @link https://www.paidmembershipspro.com/add-ons/pmpro-register-helper-add-checkout-and-profile-fields/
*
* 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 my_pmprorh_init_example_fields_and_custom_checkout_boxes() {
// don't break if Register Helper is not loaded
if ( ! function_exists( 'pmprorh_add_registration_field' ) ) {
return false;
}
/**
* pmprorh_add_checkout_box($name, $label = NULL, $description = "", $order = NULL)
*
* Create custom checkout box
*
* @param string $name lowercase alphabetical characters and underscores only, no spaces or special characters.
* @param string $label (optional) section title displayed.
* @param string $description (optional) descriptive paragraph displayed below section title.
* @param integer $order (optional) positive or negative number, e.g. -1, 0, or 1. The default checkout_boxes location order is 0.
* @return void
*/
pmprorh_add_checkout_box( 'foo' );
pmprorh_add_checkout_box( 'bar', 'Bar' );
pmprorh_add_checkout_box( 'quz', 'Quz', 'Description of the Quz section. With order set to 1 this will be one after the default checkout_boxes location.', 1 );
pmprorh_add_checkout_box( 'baz', 'Baz', 'Description of the Baz section. With order set to -1 this will be one before the default checkout_boxes location.', -1 );
// define the fields
$fields = array();
// This field contains a custom location option in the array and should be created in the checkbox location if it was created.
$fields[] = new PMProRH_Field(
'example_one', // input field name, used as meta key
'text', // field type
array(
'label' => 'Example One', // display custom label, if not used field name will be used
'size' => 30, // field width
'profile' => true, // show on profile
'memberslistcsv' => true, // include when using export members to csv
'addmember' => true, // include when using add member from admin
// 'required' => true, // make field required
'location' => 'foo', // Create field in custom location
)
);
// This field contains another custom location option in the array and should be created in the checkbox location if it was created.
$fields[] = new PMProRH_Field(
'example_two', // input field name, used as meta key
'text', // field type
array(
'label' => 'Example Two', // display custom label, if not used field name will be used
'size' => 50, // field width
'profile' => true, // show on profile
'memberslistcsv' => true, // include when using export members to csv
'addmember' => true, // include when using add member from admin
// 'required' => true, // make field required
'location' => 'bar', // Create field in custom location
)
);
// This field contains a location option that is not set (does not exist) in the array and should default to the default set location.
$fields[] = new PMProRH_Field(
'example_three', // input field name, used as meta key
'text', // field type
array(
'label' => 'Example Three', // display custom label, if not used field name will be used
'size' => 10, // field width
'profile' => true, // show on profile
'memberslistcsv' => true, // include when using export members to csv
'addmember' => true, // include when using add member from admin
// 'required' => true, // make field required
'location' => 'other', // Create field in custom location
)
);
// This field does not contain a location option in the array and should default to the default set location.
$fields[] = new PMProRH_Field(
'example_four', // input field name, used as meta key
'text', // field type
array(
'label' => 'Example Four', // display custom label, if not used field name will be used
'size' => 20, // field width
'profile' => true, // show on profile
'memberslistcsv' => true, // include when using export members to csv
'addmember' => true, // include when using add member from admin
// 'required' => true, // make field required
)
);
// This field contains another custom location option in the array and should be created in the checkbox location if it was created.
$fields[] = new PMProRH_Field(
'example_five', // input field name, used as meta key
'text', // field type
array(
'label' => 'Example Five', // display custom label, if not used field name will be used
'size' => 40, // field width
'profile' => true, // show on profile
'memberslistcsv' => true, // include when using export members to csv
'addmember' => true, // include when using add member from admin
// 'required' => true, // make field required
'location' => 'quz', // Create field in custom location
)
);
// This field contains another custom location option in the array and should be created in the checkbox location if it was created.
$fields[] = new PMProRH_Field(
'example_six', // input field name, used as meta key
'text', // field type
array(
'label' => 'Example Six', // display custom label, if not used field name will be used
'size' => 5, // field width
'profile' => true, // show on profile
'memberslistcsv' => true, // include when using export members to csv
'addmember' => true, // include when using add member from admin
// 'required' => true, // make field required
'location' => 'baz', // Create field in custom location
)
);
foreach ( $fields as $field ) {
// set default location
$location = 'checkout_boxes';
// set custom location if a valid RH checkout box
if ( ! empty( $field->location ) && pmprorh_getCheckoutBoxByName( $field->location ) ) {
$location = $field->location;
}
pmprorh_add_registration_field(
$location, // location on checkout page
$field // PMProRH_Field object
);
}
unset( $field );
// that's it. see the PMPro Register Helper readme for more information and examples.
}
add_action( 'init', 'my_pmprorh_init_example_fields_and_custom_checkout_boxes' );
@ipokkel
Copy link
Author

ipokkel commented Dec 17, 2020

201217-1608198984

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