Support

Account

Home Forums Add-ons User Submitted ACF Country field

Solved

ACF Country field

  • Hi,

    I needed a country field for a project I’m working on so I quickly developped an addon for this :
    https://github.com/nlemoine/acf-country

    This addon displays an ISO 3166-1 alpha 2 country list. Country names are available in every language and probably in your WordPress installation language.

    This plugin requires :
    – ACF v4+
    – PHP 5.3.3+

    Beware, the weight of this plugin is about 10mo.

    I plan to improve a little bit this plugin later for greater compatibility (ACF 3, etc.).

    Hope it will help someone else !

    • Elliot

    • January 7, 2014 at 10:17 am

    Hi @nlemoine

    Great work!

    Thanks
    E

    • Rneeson

    • November 17, 2014 at 1:10 pm

    I just downloaded the file and installed it all went good but when i went to insert the
    include_once(‘acf-country/acf-country.php’);
    into the php file it came up with an error and wont let me insert it?
    where do i insert it into the file sorry for being so stupid please see your code below thanks in advance

    <?php
    /*
    Plugin Name: Advanced Custom Fields: Country Field
    Plugin URI: https://github.com/Vheissu/acf-country-field
    Description: Adds a country as well as city/state field to your WordPress sites.
    Version: 1.0.0
    Author: Dwayne Charrington
    Author URI: http://dwaynecharrington.com
    License: GPL
    */

    class acf_field_country_plugin
    {

    public function __construct()
    {
    $domain = “acf-country-field”;
    $mofile = trailingslashit(dirname(__File__)) . ‘lang/’ . $domain . ‘-‘ . get_locale() . ‘.mo’;
    load_textdomain( $domain, $mofile );

    add_action(‘acf/register_fields’, array($this, ‘register_fields’));

    register_activation_hook( __FILE__, array($this, ‘populate_db’) );
    register_deactivation_hook( __FILE__, array($this, ‘depopulate_db’) );
    }

    public function register_fields()
    {
    include_once ‘register-fields.php’;
    }

    public function populate_db()
    {
    require_once(ABSPATH . ‘wp-admin/includes/upgrade.php’);

    ob_start();
    require_once “lib/install-data.php”;
    $sql = ob_get_clean();
    dbDelta( $sql );
    }

    public function depopulate_db()
    {
    require_once(ABSPATH . ‘wp-admin/includes/upgrade.php’);

    ob_start();
    require_once “lib/drop-tables.php”;
    $sql = ob_get_clean();
    dbDelta( $sql );
    }

    }

    new acf_field_country_plugin();

  • Hello,

    You’re adressing your issue to the wrong plugin. Check the Github URL in the first post.

  • Hi there,
    This is just what I needed. However, will this also work with ACF Pro?
    Thanks

    edit: Unfortunately it doesn’t work with ACF Pro

  • Unfortunately I can confirm that. When activating the plugin, an error occurs.

    Fatal error: Cannot redeclare composerRequire …

    It’s a pitty, this one would have been handy for me just now.

  • For this plugin: https://github.com/Vheissu/acf-country-field
    I have add ACF field to woocommerce product. Countries field is visible, country chosing works properly, but when save produkt and page reload country list looks like default view. Should display selected country. Not save selected country.

  • Hi,

    I just updated the plugin, it’s been improved and is now fully functionnal with ACF 4 & 5 (pro).

    You can check it here: https://github.com/nlemoine/acf-country

  • Hi,

    Works great on my site!

    Just a suggestion if you can add states for each country chosen.
    The 1st option to choose is country. Once country is chosen, only states from that chosen country appear for 2nd option.

    Thanks.

  • Hi,

    Sorry, this field aims to only display countries.

    Adding states would also involve some issues.
    States are a US administrative concept, they are called “lander” in Germany, “region” which has a subdivision called “departement”. At a world scale, it would be quite hard to handle every country specificities.

    If you want state option, you can install the other ACF country plugin: https://github.com/Vheissu/acf-country-field

    • Spen

    • November 2, 2017 at 9:13 am

    Great plugin @nlemoine – the country flags are not loading inside the select, though. Any idea? All of the assets appear to be loading correctly.

    Cheers 🙂

    • Beee

    • November 5, 2017 at 3:44 am

    Nice feature with the flags.

    For those of you that might be looking for a more extensive location selection, I’ve just released v0.1 of the ACF City Selector plugin where you can select a country, the a state (within that country) and then a city (within that state).

    Read more about it on http://acfcs.berryplasman.com

  • Wow, that looks impressive, Berry. I definitely keep an eye on this!

  • @nlemoine I want to show value of country was chosen from back-end to front-end

    i was using this code

    get_field(‘country_selling’); // country_selling are field name when setup

    but it show value NULL

    can you guide me how to show value of country was chosen.

    Thank

  • @trantin2310@gmail.com If you are using both “Country code” & “Country name”, it returns an array so you should see the “Working with Array values” section of this ressource https://www.advancedcustomfields.com/resources/code-examples/

  • @nlemoine exactly what i was looking for, simple and efficient.
    I am using the Country code and name (as array) option and i want to to display the country code in the target destination of the link (example : site.com/films/pays=FR) but the country name in the source (example : France).

    <?php
        $pays_values = get_field('pays');
        if ($pays_values) {
            foreach ($pays_values as $pays_value) { ?>
            <a href="<?php echo esc_url( home_url( '/' ) ); ?>films?pays=<?php echo $pays_value; ?>"><?php echo $pays_value; ?></a>
            <?php }
        }
    ?>

    As a result, clicking on the word France would point to http://www.mysite.com/films/pays=FR
    But i don’t know how to do this. Maybe someone could explain ? I think it’s a matter of how to retrieve a specific data from an array but i’m stuck.
    Thanks.

  • Assuming $pays_value is an array like array('fr' => 'France'):

    
    <?php
        $pays_values = get_field('pays');
        if ($pays_values) {
            foreach ($pays_values as $pays_value) { ?>
            <a href="<?php echo esc_url( home_url( '/' ) ); ?>films?pays=<?php echo key($pays_value); ?>"><?php echo array_values($pays_value)[0]; ?></a>
            <?php }
        }
    ?>
    

    Just grab the key of the array.

  • Thanks for your answer ! I have to tell you i’m using the “Multiple possible values” option and making a var_dump($pays_values); outputs me : array(2) { [“DE”]=> string(9) “Allemagne” [“FR”]=> string(6) “France” }. It seems far from what should be output as you mentioned (array(‘fr’ => ‘France’, ‘de’ => ‘Allemagne’)). Sorry if it seems obvious, i’m not very confortable with arrays.

  • Arrays are exactly the same. When you var_dump, some informations are printed (for debugging purposes) on the length of the array (2 items), and the type and length of values (string with X characters).

    array(2) { [“DE”]=> string(9) “Allemagne” [“FR”]=> string(6) “France” }
    equals:
    array('FR' => 'France', 'DE' => 'Allemagne')

    The answer I suggested should work in that case.

  • It makes more sense to me now 🙂 So i’m asking myself why nothing is returned, i’ll come back to you when i’d check everything, don’t want to bother you all day. Thanks again.

    • studiok7

    • September 4, 2018 at 11:04 pm

    Unfortunately, i was not able to return anything with this code and “Multiple possible values” option :

    <?php
        $pays_values = get_field('pays');
        if ($pays_values) {
            foreach ($pays_values as $pays_value) { ?>
            <a href="<?php echo esc_url( home_url( '/' ) ); ?>films?pays=<?php echo key($pays_value); ?>"><?php echo array_values($pays_value)[0]; ?></a>
            <?php }
        }
    ?>

    However, without the foreach argument and with “Multiple possible values” option disabled, it works as expected for one country (for record, clicking on the word France points to http://www.mysite.com/films/pays=FR

    <?php
        $pays_values = get_field('pays');
        if ($pays_values) { ?>
            <a href="<?php echo esc_url( home_url( '/' ) ); ?>films?pays=<?php echo key($pays_values); ?>"><?php echo array_values($pays_values)[0]; ?></a>
            <?php }
    ?>
  • I have tested everything i could and i think i’m not far. The only result i have with “Multiple possible values” option activated and this code returns me the name of each country but the link of each country is not with the 2 letters code. Instead of having http://www.mysite.com/films/pays=FR, i have http://www.mysite.com/films/pays=France. I see exactly what is the problem but i’m not able to write correctly “i want $pays_value(name of the country) and $pays_value(2 letters code)”.

    <?php
        $pays_values = get_field('pays');
        if ($pays_values) {
            foreach ($pays_values as $pays_value) { ?>
            <a href="<?php echo esc_url( home_url( '/' ) ); ?>films?pays=<?php echo $pays_value; ?>"><?php echo $pays_value; ?></a>
            <?php }
        }
    ?>

    These are my limits -_-

  • Sorry, your issue isn’t really ACF related and I have no time to dig further. Please have a closer to look to the code I posted.

  • thanks for your answer, i did test the code you posted but it did return “NULL”. This is everything i’ve done :
    ACF Country options
    code
    Multiple value in post
    Result + var dump
    If someone has an idea 😉

  • sorry the result is not “NULL”, in fact there is no result

    Result + var dump

Viewing 25 posts - 1 through 25 (of 25 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.