Skip Navigation

[Resolved] Setting multiple options (all the countries in the world) for a select field

This support ticket is created 5 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 5 replies, has 4 voices.

Last updated by alejod 5 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#1215499

I want to add a select field to a custom post type that allows the user to select from any of the countries in the world. How can I add this without typing them in one by one?

Thank you!

#1216718

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - do you have a database table for countries that holds all countries entry?

#1218277

No, I just have them in a list.

#1218828

Hi,

Minesh is on vacation, your will get the answer in tomorrow morning

#1219399

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

When you say, list - what kind of list you have?

There is no such feature available but you can add user custom field as select and later you can use Types hook "wpt_field_options" to fill out the dropdown option for your county dropdown box dynamically.

I suggest creating a country table within your database using the following country list sql:
=> https://github.com/raramuridesign/mysql-country-list/blob/master/mysql-country-list.sql

And then create a custom select field "country-list" and add the following field to your:

For example - add the following code to your current theme's function.php file OR you can add it to the "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

add_filter( 'wpt_field_options', 'func_dynamic_populate', 10, 3); 
function func_dynamic_populate( $options, $title, $type ){
global $wpdb;

    switch( $title ){
        case 'country-list':
            $options = array();

     // replace your table name iwth myTable
    $result = $wpdb->get_results ( "SELECT * FROM myTable" );           

 $posts_array = $result
            foreach ($posts_array as $post) {
                $options[] = array(
                    '#value' => $post->ID,
                    '#title' => $post->country_name,
                );
            }
            break;
    }
    return $options;
}

Where:
=> You should change 'contry-list' to your custom field name if required.
=> Feel free to adjust the above as as required, if needed.

#1582939

Hello. Could somebody enable the hidden links in Minesh response please. https://toolset.com/forums/topic/setting-multiple-options-all-the-countries-in-the-world-for-a-select-field/#post-1219399
thanks