Forked from strangerstudios/get_user_metadata_pmpro_prepop.php
Last active
December 14, 2023 14:19
-
-
Save dwanjuki/1e760e2eb82c8c89e601e530894c263c to your computer and use it in GitHub Desktop.
Default some PMPro fields based on other user meta.
This file contains hidden or 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
<?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