Site Builder
Editing:
Geo-JSON-Exporter.txt
writable 0666
<?php /* Plugin Name: Geo JSON Exporter Description: Exports a JSON file per US state from the city table for fast AJAX geo navigation. Output goes to /geo/json-data/{STATE}.json. Version: 1.0 Author: BestDealOn Dev Team */ if ( ! defined('ABSPATH') ) exit; define('GJE_TABLE', 'kVTr1OdFj_city'); // Change to your table name if needed define('GJE_OUTDIR', $_SERVER['DOCUMENT_ROOT'] . '/geo/json-data/'); // Ensure output directory exists if (!is_dir(GJE_OUTDIR)) mkdir(GJE_OUTDIR, 0775, true); add_action('admin_menu', function() { add_management_page( 'Geo JSON Exporter', 'Geo JSON Exporter', 'manage_options', 'geo_json_exporter', 'gje_admin_page' ); }); function gje_admin_page() { global $wpdb; $states = $wpdb->get_col("SELECT DISTINCT subdiv_id FROM " . GJE_TABLE . " WHERE country_id='USA' ORDER BY subdiv_id"); ?> <div class="wrap" id="gje-app"> <h1>Geo JSON Exporter</h1> <p>This tool exports one <code>.json</code> file per state into <code>/geo/json-data/</code> for ultra-fast city/county navigation.</p> <div> <label for="gje-state"><b>State:</b></label> <select id="gje-state"> <option value="">All States</option> <?php foreach($states as $state): ?> <option value="<?php echo esc_attr($state); ?>"><?php echo esc_html($state); ?></option> <?php endforeach; ?> </select> <button id="gje-export" class="button button-primary">Export</button> </div> <div id="gje-status" style="margin-top:20px;font-family:monospace;white-space:pre;"></div> </div> <script> (function($){ $('#gje-export').on('click', function(){ var state = $('#gje-state').val(); $('#gje-status').text('Working...'); $.post(ajaxurl, { action: 'gje_export_json', state: state }, function(data){ if (data.success) { $('#gje-status').text(data.data.status); } else { $('#gje-status').text('Error: ' + (data.data ? data.data.status : 'Unknown error')); } }); }); })(jQuery); </script> <?php } // AJAX: Export JSON (single state or all) add_action('wp_ajax_gje_export_json', function() { global $wpdb; $state = isset($_POST['state']) ? sanitize_text_field($_POST['state']) : ''; $outdir = GJE_OUTDIR; $status = ''; $states = $state ? [$state] : $wpdb->get_col("SELECT DISTINCT subdiv_id FROM " . GJE_TABLE . " WHERE country_id='USA'"); foreach ($states as $st) { $rows = $wpdb->get_results($wpdb->prepare( "SELECT city_title, city_lat, city_lon, city_id FROM " . GJE_TABLE . " WHERE country_id='USA' AND subdiv_id=%s", $st )); $arr = []; foreach($rows as $r) { $slug = preg_replace('/[^a-z0-9]+/i','-', strtolower($r->city_title)); $arr[] = [ 'city' => $r->city_title, 'lat' => floatval($r->city_lat), 'lon' => floatval($r->city_lon), 'slug' => $slug, 'url' => "/geo/USA/{$st}/{$slug}/" ]; } $outfile = $outdir . $st . '.json'; file_put_contents($outfile, json_encode($arr, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); $status .= "Exported " . count($arr) . " cities to $outfile\n"; } wp_send_json_success(['status' => $status]); });
Save changes
Create folder
writable 0777
Create
Cancel