Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Benjamin Pick

    (@benjamin4)

    Sorry the JS API is not ready yet. I am working on it at https://github.com/yellowtree/wp-geoip-detect/tree/ajax .

    Thread Starter Ben Gray

    (@bravoechon)

    Cool, no worries. Thanks again for developing the plugin.

    @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

    (@bravoechon)

    @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.