Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save JarrydLong/80cc389ae569d117a4703463803852f0 to your computer and use it in GitHub Desktop.

Select an option

Save JarrydLong/80cc389ae569d117a4703463803852f0 to your computer and use it in GitHub Desktop.
<?php //do not copy
/**
* This recipe will geocode users and ensure compatibility with Membership Maps v0.9+
* Run /wp-admin/?pmpromm_process_users=true to run the script.
*
* 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_users'] ) ){
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 ){
//Change the field values to match the address fields you are using.
//Leave fields that you are not using as empty strings.
$member_address = array(
'street' => get_user_meta( $result->ID, 'pmpro_baddress1', true ),
'city' => get_user_meta( $result->ID, 'pmpro_bcity', true ),
'state' => get_user_meta( $result->ID, 'pmpro_bstate', true ),
'zip' => get_user_meta( $result->ID, 'pmpro_bzipcode', true )
);
$coordinates = pmpromm_geocode_address( $member_address );
$member_address['optin'] = true;
if( is_array( $coordinates ) ){
if( !empty( $coordinates['lat'] ) && !empty( $coordinates['lng'] ) ){
$member_address['latitude'] = $coordinates['lat'];
$member_address['longitude'] = $coordinates['lng'];
//Only add to user meta if the address has been geocoded
update_user_meta( $result->ID, 'pmpromm_pin_location', $member_address );
} else {
//Cleanup pin location in case
delete_user_meta( $result->ID, 'pmpromm_pin_location' );
}
}
}
exit('End');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment