Using the new WP-CLI command to import location data from a CSV file

In version 1.5.0 of Cardinal Store Locator new import functionality is available via a custom WP-CLI command. At this point the import functionality is somewhat basic but additional features will be added in the future to support more file types and options.

Two new filters are available specific to importing:

  • bh_sl_import_file_args: Allows you to change the mapping structure of your CSV file to the columns that Cardinal expects.
  • bh_sl_import_post_meta: Allows you to add additional custom meta fields if you have additional location data that’s not covered by the default columns.

Steps to import locations from a CSV file

  1. WP-CLI access
    Ensure you have access to WP-CLI on your hosting platform and/or your local development environment. If WP-CLI is not available on your hosting provider’s platform it’s still possible to import the locations locally with WP-CLI, export the locations with the WordPress core export functionality under WordPress Dashboard > Tools > Export > Locations > download the XML file and then use WordPress Dashboard > Tools > Import > WordPress > Run Importer on your live site.

    For a local development environment WP-CLI can be installed manually or it comes bundled with things such as VVV and WP Local Docker.
  2. Format CSV location data
    It’s highly recommended to use this starter CSV template to format your location data. If you have an existing sheet and can’t or don’t want to change the column names, it is possible to map your columns to what the plugin is expecting using the bh_sl_import_file_args filter.

    If your locations are already geocoded with latitude and longitude coordinates please add them as “lat” and “lng” columns to your sheet. The import functionality will attempt to geocode the locations on import but if you’ve already had them geocoded you can avoid additional geocoding requests tallying against your Google Cloud account.

    If you have location categories and there are locations that are assigned to multiple, you’ll need to make sure they are separated by commas.
  3. Upload or move the CSV to the web root or path within the site structure, such as the wp-content directory
    Copy or upload your CSV file to your site in a path where WordPress files are stored and copy the name of the file.
  4. Run the import command
    After verifying that WP-CLI is installed and working, the command to run the import from the CSV file is:

    wp cardinal-locator import <locations-import.csv>

    Replace <locations-import.csv> above with the name of your CSV file.

    Running the command will loop through all the locations (rows) in the CSV file and will pause briefly on occasion for memory management.

Once the import process has completed a small report will display at the bottom with the number of locations that were created and skipped. Rows are skipped if required address information is missing or a duplicate location is detected. Duplicates are determined by a matching name and address and in this initial version, there isn’t a way to overwrite duplicates. Overwriting functionality will be added in a future version of the plugin.