Skip to content

Instantly share code, notes, and snippets.

@JarrydLong
Last active November 10, 2021 08:37
Show Gist options
  • Save JarrydLong/f1f65560cccd067b04e98265c87daf39 to your computer and use it in GitHub Desktop.
Save JarrydLong/f1f65560cccd067b04e98265c87daf39 to your computer and use it in GitHub Desktop.
<?php
/**
* This recipe will geocode existing member locations. Add /?pmpromm_process=true
* to run this script from /wp-admin/
*
* Change line 26 to increase batch sizes. Note that the Google Maps Geocode API
* has a daily limit of 2 000 requests.
*
* 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 mypmpromm_batch_process_addresses_override(){
if( !empty( $_REQUEST['pmpromm_process'] ) ){
mypmpromm_batch_process_addresses();
}
}
add_action( 'admin_init', 'mypmpromm_batch_process_addresses_override' );
function mypmpromm_batch_process_addresses(){
global $wpdb;
$batch_size = 30; //Number of members to geocode at a time
$api_key = pmpro_getOption( 'pmpromm_api_key' );
if( !empty( $api_key ) && function_exists( 'pmpromm_geocode_address' ) ){
$sql = "SELECT ord.user_id, ord.billing_street, ord.billing_city, ord.billing_state, ord.billing_zip
FROM $wpdb->pmpro_membership_orders ord
LEFT JOIN $wpdb->usermeta um ON um.user_id = ord.user_id AND um.meta_key = 'pmpro_lat'
WHERE um.meta_key is null OR um.meta_value = null
AND ord.billing_street != ''
AND ord.status = 'success'
LIMIT $batch_size
";
$results = $wpdb->get_results( $sql );
foreach( $results as $result ){
$member_address = array(
'street' => $result->billing_street,
'city' => $result->billing_city,
'state' => $result->billing_state,
'zip' => $result->billing_zip
);
$coordinates = pmpromm_geocode_address( $member_address );
//Geocode was successful, add to user meta
if( is_array( $coordinates ) ){
update_user_meta( intval( $result->user_id ), 'pmpro_lat', $coordinates['lat'] );
update_user_meta( intval( $result->user_id ), 'pmpro_lng', $coordinates['lng'] );
}
}
exit('End');
}
}
@laurenhagan0306
Copy link

This recipe is included in the blog post on "Batch Geocode Existing Members for the Membership Maps Add On" at Paid Memberships Pro here: https://www.paidmembershipspro.com/batch-geocode-existing-members-for-the-membership-maps-add-on/

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