Last active
July 10, 2023 08:53
-
-
Save MaryOJob/c0c17d0c8dba5bac39dc123b38013079 to your computer and use it in GitHub Desktop.
Multiple Location / Headers for Custom User Fields Created with Code for Paid Memberships Pro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // Do not copy this line | |
/** | |
* Add user fields using code with multiple headers. | |
* This example will add a Business, Study and Laptop Information section to your checkout and profile pages | |
* | |
* title: Add user fields using code. | |
* collection: user-fields | |
* category: custom-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_pmpro_add_user_fields_mary() { | |
// Don't break if PMPro is out of date or not loaded. | |
if ( ! function_exists( 'pmpro_add_user_field' ) ) { | |
return false; | |
} | |
// Add a field group to put our fields into. | |
pmpro_add_field_group( 'personal', 'Personal Details' ); | |
pmpro_add_field_group( 'study', 'Study Details' ); | |
pmpro_add_field_group( 'laptop', 'Laptop Details' ); | |
// Define the fields. | |
$fields_personal = array(); | |
$fields_personal[] = new PMProRH_Field( | |
'inputname1', // input name, will also be used as meta key | |
'text', // type of field | |
array( | |
'label' => 'Label Name 1', // custom field label | |
'size' => 40, // custom field box size | |
'profile' => true, // show in user profile | |
'required' => true, // make this field required | |
'memberslistcsv' => true, // add to Member List export CSV file | |
) | |
); | |
$fields_personal[] = new PMProRH_Field( | |
'inputname2', // input name, will also be used as meta key | |
'text', // type of field | |
array( | |
'label' => 'Label Name 2', // custom field label | |
'size' => 40, // custom field box size | |
'profile' => true, // show in user profile | |
'required' => true, // make this field required | |
) | |
); | |
$fields_study = array(); | |
$fields_study[] = new PMProRH_Field( | |
'inputname3', // input name, will also be used as meta key | |
'text', // type of field | |
array( | |
'label' => 'Label Name 3', // custom field label | |
'size' => 40, // custom field box size | |
'profile' => true, // show in user profile | |
'required' => true, // make this field required | |
) | |
); | |
$fields_study[] = new PMProRH_Field( | |
'inputname4', // input name, will also be used as meta key | |
'text', // type of field | |
array( | |
'label' => 'Label Name 4', // custom field label | |
'size' => 40, // custom field box size | |
'profile' => true, // show in user profile | |
'required' => true, // make this field required | |
) | |
); | |
$fields_study[] = new PMProRH_Field( | |
'inputname5', // input name, will also be used as meta key | |
'text', // type of field | |
array( | |
'label' => 'Label Name 5', // custom field label | |
'size' => 40, // custom field box size | |
'profile' => true, // show in user profile | |
'required' => true, // make this field required | |
) | |
); | |
$fields_laptop = array(); | |
$fields_laptop[] = new PMProRH_Field( | |
'inputname6', // input name, will also be used as meta key | |
'text', // type of field | |
array( | |
'label' => 'Label Name 6', // custom field label | |
'size' => 40, // custom field box size | |
'profile' => true, // show in user profile | |
'required' => true, // make this field required | |
) | |
); | |
$fields_laptop[] = new PMProRH_Field( | |
'inputname7', // input name, will also be used as meta key | |
'text', // type of field | |
array( | |
'label' => 'Label Name 7', // custom field label | |
'size' => 40, // custom field box size | |
'profile' => true, // show in user profile | |
'required' => true, // make this field required | |
) | |
); | |
// Add all of our fields into their groups. | |
foreach ( $fields_personal as $field ) { | |
pmpro_add_user_field( | |
'personal', // Which group to add to. | |
$field // PMPro_Field object | |
); | |
} | |
foreach ( $fields_study as $field ) { | |
pmpro_add_user_field( | |
'study', // Which group to add to. | |
$field // PMPro_Field object | |
); | |
} | |
foreach ( $fields_laptop as $field ) { | |
pmpro_add_user_field( | |
'laptop', // Which group to add to. | |
$field // PMPro_Field object | |
); | |
} | |
// That's it. See the PMPro User Fields docs for more information. | |
} | |
add_action( 'init', 'my_pmpro_add_user_fields_mary' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment