add_action(‘wp_enqueue_scripts’, function () {
wp_enqueue_script(‘geo-script’, get_template_directory_uri() . ‘/geo.js’, [], null, true);
wp_localize_script(‘geo-script’, ‘geo_ajax’, [
‘ajax_url’ => admin_url(‘admin-ajax.php’),
‘nonce’ => wp_create_nonce(‘geo_nonce’)
]);
});
add_action(‘wp_ajax_send_location’, ‘handle_send_location’);
add_action(‘wp_ajax_nopriv_send_location’, ‘handle_send_location’);
function handle_send_location() {
check_ajax_referer(‘geo_nonce’, ‘security’);
$lat = sanitize_text_field($_POST[‘lat’]);
$lon = sanitize_text_field($_POST[‘lon’]);
// Send email
$message = “Someone shared their location:\nLatitude: $lat\nLongitude: $lon\nGoogle Maps: https://maps.google.com/?q=$lat,$lon”;
wp_mail(get_option(‘admin_email’), ‘Location Captured’, $message);
wp_send_json_success(“Location sent successfully.”);
}