Search by

arraypress / wp-countries

arraypress

A lightweight WordPress library for working with country codes and names with translation support.

Package info

github.com/arraypress/wp-countries

pkg:composer/arraypress/wp-countries

Statistics

Installs: 833

Dependents: 3

Suggesters: 1

Stars: 0

Open Issues: 0

v1.0.0 2026-08-25 17:48 UTC

This package is auto-updated.

Last update: 2026-09-02 16:26:10 UTC


README

Country codes, names and continents, with the groupings a shop actually needs.

What it does

Every store form needs a country dropdown, and every stored order needs a two-letter code turned back into a name. Beyond that it is always the same questions: is this in the EU for VAT, which continent is it, and is it on the list we treat with more caution.

Names go through WordPress translation, so the dropdown follows the site language rather than being English-only.

Features

  • Look up a country name from its code, and a code from its name
  • Build a ready-made options array for a <select>
  • Look up the states or provinces of a country, for a dependent dropdown
  • Search by partial name, for an autocomplete
  • Check EU membership, for VAT decisions
  • Group by continent, or ask which continent a country is in
  • Flag a country as higher risk, for fraud rules
  • Get the flag emoji for a code
  • Sanitise a user-supplied code down to a real one, or null

Installation

composer require arraypress/wp-countries

Quick start

use ArrayPress\Countries\Countries;
use ArrayPress\Countries\Regions;

// A country dropdown.
foreach ( Countries::all() as $code => $name ) {
    printf( '<option value="%s">%s</option>', esc_attr( $code ), esc_html( $name ) );
}

// The provinces for the country picked, for a dependent dropdown.
$regions = Regions::for( 'CA' );  // [ 'AB' => 'Alberta', 'BC' => 'British Columbia', ... ]

// Turn a stored code back into something readable.
echo esc_html( Countries::get_name( $order->billing_country ) );  // United Kingdom

// VAT applies?
if ( Countries::is_eu( $order->billing_country ) ) {
    // ...
}

// Never trust a posted code.
$code = Countries::sanitize( $_POST['country'] ?? '' );

Requirements

  • PHP 8.3 or later
  • WordPress 7.1 or later

License

GPL-2.0-or-later