Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kimwhite/62f7359dda67ff6fe444bf8d7c87b8f1 to your computer and use it in GitHub Desktop.
Save kimwhite/62f7359dda67ff6fe444bf8d7c87b8f1 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/
*
* 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_users(){
if( !empty( $_REQUEST['pmpromm_process'] ) ){
mypmpromm_batch_process_addresses_users();
}
}
add_action( 'admin_init', 'mypmpromm_batch_process_addresses_override_users' );
function mypmpromm_batch_process_addresses_users(){
global $wpdb;
$api_key = pmpro_getOption( 'pmpromm_api_key' );
if( !empty( $api_key ) && function_exists( 'pmpromm_geocode_address' ) ){
$users = get_users();
foreach( $users as $result ){
$member_address = array(
'street' => get_user_meta( $result->ID, 'pmpro_baddress1', true ).' '.get_user_meta( $result->ID, 'pmpro_baddress2', true ),
'city' => get_user_meta( $result->ID, 'city', true ),
'state' => get_user_meta( $result->ID, 'pmpro_bstate', true ),
'zip' => get_user_meta( $result->ID, 'zip_code', true )
);
$coordinates = pmpromm_geocode_address( $member_address );
if( is_array( $coordinates ) ){
update_user_meta( intval( $result->ID ), 'pmpro_lat', $coordinates['lat'] );
update_user_meta( intval( $result->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