Files
2026-10-03 13:51:47 +02:00

3.4 KiB
Raw Permalink Blame History

If you need to create a map on Wordpress with around many markers, the following script should help you.

The Python script will:

  • Read name,address rows from a CSV file.
  • Send each address to Mapbox’s forward-geocoding API.
  • Extract the returned place name, Mapbox feature ID, longitude, and latitude.
  • Generate a complete wp:jetpack/map block that you can paste into the WordPress code editor.

Mapbox’s geocoder converts address text into coordinates, and its response includes a feature ID and human-readable place name suitable for constructing the Jetpack point objects.

Expected CSV

Save a file called addresses.csv:

name,address
Alice,"1 rue de la Paix, Paris, France"
Bob,"3 avenue Foch, Paris, France"

The first column becomes the marker title. The second column is sent to Mapbox.

Running it

Install the required library:

python -m pip install requests

Set your Mapbox token.

On macOS/Linux:

export MAPBOX_TOKEN="pk.your_mapbox_token_here"

On Windows PowerShell:

$env:MAPBOX_TOKEN="pk.your_mapbox_token_here"

Run the script:

python generate_jetpack_map.py addresses.csv

It will create:

jetpack-map.txt

You can also specify the output filename:

python generate_jetpack_map.py addresses.csv \
  --output wordpress-map.txt \
  --country fr \
  --language fr \
  --zoom 12

The generated file will contain a block that you can paste into Wordpress. Wordpress will complain that the element needs to be repaired, but it should work.

The script calculates mapCenter as the average longitude and latitude of all successfully geocoded addresses.

A few practical details:

  • title comes from the CSV name column.
  • placeTitle and caption come from Mapbox’s returned address.
  • Failed addresses are excluded from the map and written to a separate *-failed.csv file.
  • The token is read from an environment variable rather than being written into the script.
  • If your addresses are not in France, change --country, for example --country be or --country us.
  • If you do not want Mapbox to return French place names, use --language en.

The permanent=True setting in the script is intentional because the coordinates and Mapbox IDs are being saved into WordPress. If your Mapbox account does not support permanent geocoding, remove that setting only after confirming that your intended storage and use comply with Mapbox’s current terms.

I also needed a helper to split addresses as my initial file sometimes contrained two addresses on the same line.

The script will convent

name,address
"Title 1 / Title 2","Address 1 / Address 2"
"Title 3","Address 3"

into:

name,address
Title 1,Address 1
Title 2,Address 2
Title 3,Address 3

Run it like this:

python split_addresses.py addresses.csv addresses-normalized.csv

The script pairs values by position:

Title 1 / Title 2
Address 1 / Address 2

becomes:

Title 1 → Address 1
Title 2 → Address 2

It also checks that each row contains the same number of titles and addresses. For example, this input will produce an error rather than silently pairing incorrectly:

name,address
"Title 1 / Title 2","Address 1"

This assumes / is always your separator. If an address can legitimately contain a slash, such as 12 A/B Main Street, that row would need a different delimiter or special handling.