Skip to content

Instantly share code, notes, and snippets.

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 MaryOJob/902118b7327d528c5e8cf14cd8b4e94b to your computer and use it in GitHub Desktop.
Save MaryOJob/902118b7327d528c5e8cf14cd8b4e94b to your computer and use it in GitHub Desktop.
Sync BuddyPress Profile Fields with PMPro using Register Helper
<?php
/**
* Based on the Register Helper example.
* We've added a "buddypress" option for each field
* set to the XProfile name we used when setting up
* the fields in the BuddyPress extended profile.
* If the PMPro BuddyPress Add On is activated
* then the fields will be synchronized.
* Register Helper: https://www.paidmembershipspro.com/add-ons/pmpro-register-helper-add-checkout-and-profile-fields/
* PMPro BuddyPress: https://www.paidmembershipspro.com/add-ons/buddypress-integration/
*/
function 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(
'buddypress' => 'Company', // XProfile Field Name <-- !!!
'label' => 'Company', // custom field label
'size' => 40, // input size
'class' => 'company', // custom class
'profile' => true, // show in user profile
'required' => true, // make this field required
)
);
$fields[] = new PMProRH_Field(
'name_of_product_or_service', // input name, will also be used as meta key
'text', // type of field
array(
'buddypress' => 'Name Of Product Or Service', // XProfile Field Name <-- !!!
'label' => 'Name Of Product Or Service', // custom field label
'class' => 'name_of_product_or_service', // custom class
'profile' => true, // show in user profile
'required' => true, // make this field required
)
);
$fields[] = new PMProRH_Field(
'website', // input name, will also be used as meta key
'text', // type of field
array(
'buddypress' => 'Website', // XProfile Field Name <-- !!!
'label' => 'Website', // custom field label
'size' => 80, // input size
'class' => 'website', // custom class
'profile' => true, // show in user profile
'required' => true, // make this field required
)
);
$fields[] = new PMProRH_Field(
'city', // input name, will also be used as meta key
'text', // type of field
array(
'buddypress' => 'City', // XProfile Field Name <-- !!!
'label' => 'City', // custom field label
'size' => 40, // input size
'class' => 'city', // custom class
'profile' => true, // show in user profile
'required' => true, // make this field required
)
);
$fields[] = new PMProRH_Field(
'state', // input name, will also be used as meta key
'text', // type of field
array(
'buddypress' => 'State', // XProfile Field Name <-- !!!
'label' => 'State', // custom field label
'size' => 40, // input size
'class' => 'state', // custom class
'profile' => true, // show in user profile
'required' => true, // make this field required
)
);
// add the fields into a new checkout_boxes are of the checkout page
foreach ( $fields as $field ) {
pmprorh_add_registration_field(
'checkout_boxes', // 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', 'my_pmprorh_init' );
@wasanajones
Copy link

would it be weird to say, "Thank you, I love you for this." ?

@adhamdwikat
Copy link

that is awesome , but Iam stuck in fields of type file
i can not synchronize them with "buddypress" option :(

any help on his ?

@MaryOJob
Copy link
Author

MaryOJob commented Jul 5, 2021

@adham

that is awesome , but Iam stuck in fields of type file
i can not synchronize them with "buddypress" option :(

any help on his ?

Do you mean this is not working for a "file" type field?

@bjspi
Copy link

bjspi commented Jul 9, 2021

Hi MaryOJob,
this code works wonderful for pure Text fields.
But what can I do to also sync Select Dropdown Boxes, i.e. the selected values!?

In Buddypress xProfile, I have a field named "My Field xProfile", which is a dropdown with two values: "Yes", "Not available". Hence, I have this code to sync my PMPro to xProfile, but it does not work:

$field = new PMProRH_Field("My Field PMPro", "select", 
            array(
		'buddypress' => 'My Field xProfile',       // XProfile Field Name <-- !!!
            	'profile'   => false,            // show in user profile
            	"options" => array( 
                	"Yes" => "Yes", 
                	"Not available" => "Not available")));
$var = pmprorh_add_registration_field("after_email", $field);

What can I do about it?

Thanks!

@bjspi
Copy link

bjspi commented Jul 12, 2021

@MaryOJob
Do you know whether sync is possible for Dropdowns?

@MaryOJob
Copy link
Author

@bjspi it should work for dropdown fields too, I made an example here please: https://gist.github.com/MaryOJob/cae7b4f284234a714be1a6aaceb05c70

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