Created
October 29, 2015 16:10
-
-
Save strangerstudios/f0a5c4e1a2d3c5197bc1 to your computer and use it in GitHub Desktop.
Default some PMPro fields based on other user meta.
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
/* | |
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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/