Forked from JarrydLong/mypmpro-membership-maps-batch-geocode.php
Created
September 10, 2025 14:56
-
-
Save kimwhite/6ccf2c4e13ac66f734ad91e2d9bcf8f7 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* 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'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment