Title: JS API
Last modified: August 31, 2016

---

# JS API

 *  Resolved [Ben Gray](https://wordpress.org/support/users/bravoechon/)
 * (@bravoechon)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/js-api/)
 * Hi, looking at the API docs you mention that this plugin can be used with JavaScript.
 * What would be the JS function to return the visitors Country ISO code?
 * Thanks for your help and for developing such a great plugin.
 * Cheers,
 * Ben
 * [https://wordpress.org/plugins/geoip-detect/](https://wordpress.org/plugins/geoip-detect/)

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [Benjamin Pick](https://wordpress.org/support/users/benjamin4/)
 * (@benjamin4)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/js-api/#post-7318024)
 * Sorry the JS API is not ready yet. I am working on it at [https://github.com/yellowtree/wp-geoip-detect/tree/ajax](https://github.com/yellowtree/wp-geoip-detect/tree/ajax).
 *  Thread Starter [Ben Gray](https://wordpress.org/support/users/bravoechon/)
 * (@bravoechon)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/js-api/#post-7318042)
 * Cool, no worries. Thanks again for developing the plugin.
 *  [SternsbergerM](https://wordpress.org/support/users/sternsbergerm/)
 * (@sternsbergerm)
 * [9 years, 12 months ago](https://wordpress.org/support/topic/js-api/#post-7318233)
 * [@bravoechon](https://wordpress.org/support/users/bravoechon/), you could do 
   it yourself – something like (sorry it’s q&d):
 *     ```
       add_action( 'wp_ajax_wpst_get_country', 'wpst_get_country' );
       function wpst_get_country() {
   
         if(function_exists('geoip_detect2_get_info_from_current_ip'))
           $userInfo = geoip_detect2_get_info_from_current_ip();
   
         echo json_encode(array(
           'country' => $userInfo->country->name,
           'country_code' => $userInfo->country->isoCode,
           'city' => $userInfo->city->name,
         ));
   
         wp_die();
       }
       ```
   
 * and a jQuery-function like:
 *     ```
       jQuery(document).ready(function($){
   
         // Bei Formularen automatisch das Land auswählen (abhängig von IP)
         jQuery('.select-land').each(function (event) {
   
           jQuery.ajax({
             url : 'http://yourdomain.com/wp-admin/admin-ajax.php',
             type : 'post',
             dataType : 'json',
             data : {
               action : 'wpst_get_country'
             },
             error : function(jqXHT, textStatus, errorThrown) {
               console.log('Landabfrage gescheitert');
             },
   
             success : function(response) {
               console.log(response);
             }
           });
         });
       });
       ```
   
 * … and yes, you should not use the absolute path to admin-ajax.php 😉
 *  Thread Starter [Ben Gray](https://wordpress.org/support/users/bravoechon/)
 * (@bravoechon)
 * [9 years, 12 months ago](https://wordpress.org/support/topic/js-api/#post-7318234)
 * [@sternsbergerm](https://wordpress.org/support/users/sternsbergerm/), thanks 
   for this code.
 * I ended up using a PHP server-side solution.
 * Cheers,
 * Ben

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘JS API’ is closed to new replies.

 * ![](https://ps.w.org/geoip-detect/assets/icon-256x256.jpg?rev=978998)
 * [Geolocation IP Detection](https://wordpress.org/plugins/geoip-detect/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/geoip-detect/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/geoip-detect/)
 * [Active Topics](https://wordpress.org/support/plugin/geoip-detect/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/geoip-detect/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/geoip-detect/reviews/)

## Tags

 * [javascript](https://wordpress.org/support/topic-tag/javascript/)

 * 4 replies
 * 3 participants
 * Last reply from: [Ben Gray](https://wordpress.org/support/users/bravoechon/)
 * Last activity: [9 years, 12 months ago](https://wordpress.org/support/topic/js-api/#post-7318234)
 * Status: resolved