Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Created October 29, 2015 16:10
Show Gist options
  • Save strangerstudios/f0a5c4e1a2d3c5197bc1 to your computer and use it in GitHub Desktop.
Save strangerstudios/f0a5c4e1a2d3c5197bc1 to your computer and use it in GitHub Desktop.
Default some PMPro fields based on other user meta.
/*
Default some PMPro fields based on other user meta.
Add this code to your active theme's functions.php or a custom plugin.
*/
function get_user_metadata_pmpro_prepop($value, $user_id, $meta_key, $single)
{
//fields to sync, make sure there are no loops in this
$sync_fields = array(
"pmpro_bfirstname" => "first_name",
"pmpro_blastname" => "last_name",
"pmpro_baddress1" => "address",
"pmpro_bphone" => "telephone"
);
//is this a field we try to sync?
if(!empty($sync_fields[$meta_key]))
{
//get the actual meta field
$meta_cache = wp_cache_get($user_id, 'user_meta');
if ( !$meta_cache ) {
$meta_cache = update_meta_cache( 'user', array( $user_id ) );
$meta_cache = $meta_cache[$user_id];
}
//only lookup the other value if the meta field is empty
if(empty($meta_cache[$meta_key]))
{
$value = get_user_meta($user_id, $sync_fields[$meta_key], $single);
}
}
return $value;
}
add_filter('get_user_metadata', 'get_user_metadata_pmpro_prepop', 10, 4);
@laurenhagan0306
Copy link

This recipe is included in the blog post on "Pre-populate User Meta Fields with Membership Checkout Information" at Paid Memberships Pro here: https://www.paidmembershipspro.com/pre-populate-user-meta-fields-with-membership-checkout-information/

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