Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Last active October 18, 2022 20:48
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save strangerstudios/3e1d1b5ec1aa1a090a73 to your computer and use it in GitHub Desktop.
Save strangerstudios/3e1d1b5ec1aa1a090a73 to your computer and use it in GitHub Desktop.
Sync WordPress user meta fields (e.g. those added by PMPro Register Helper) with BuddyPress profile fields.
/*
Sync PMPro fields to BuddyPress profile fields.
*/
function pmprobuddy_update_user_meta($meta_id, $object_id, $meta_key, $meta_value)
{
//make sure buddypress is loaded
do_action('bp_init');
//array of user meta to mirror
$um = array(
"company" => "Company", //usermeta field => buddypress profile field
);
//check if this user meta is to be mirrored
foreach($um as $left => $right)
{
if($meta_key == $left)
{
//find the buddypress field
$field = xprofile_get_field_id_from_name($right);
//update it
if(!empty($field))
xprofile_set_field_data($field, $object_id, $meta_value);
}
}
}
add_action('update_user_meta', 'pmprobuddy_update_user_meta', 10, 4);
//need to add the meta_id for add filter
function pmprobuddy_add_user_meta($object_id, $meta_key, $meta_value)
{
pmprobuddy_update_user_meta(NULL, $object_id, $meta_key, $meta_value);
}
add_action('add_user_meta', 'pmprobuddy_add_user_meta', 10, 3);
@meninomiel
Copy link

Thanks for this code. Help me a lot to sync Buddypress extended fields with my ACF fields.

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