Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dwanjuki/1e760e2eb82c8c89e601e530894c263c to your computer and use it in GitHub Desktop.
Save dwanjuki/1e760e2eb82c8c89e601e530894c263c to your computer and use it in GitHub Desktop.
Default some PMPro fields based on other user meta.
<?php // copy from below
/**
* Synchronize WooCommerce Billing Address and PMPro Billing Address
*
* 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 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" => "billing_first_name",
"pmpro_blastname" => "billing_last_name",
"pmpro_baddress1" => "billing_address_1",
"pmpro_baddress2" => "billing_address_2",
"pmpro_bcity" => "billing_city",
"pmpro_bstate" => "billing_state",
"pmpro_bzipcode" => "billing_postcode",
"pmpro_bcountry" => "billing_country",
"pmpro_bphone" => "billing_phone",
);
//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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment