Title: Geocode Switch
Author: elialgranti
Published: <strong>2014 年 12 月 9 日</strong>
Last modified: 2018 年 7 月 21 日

---

搜尋外掛

這個外掛**並未在最新的 3 個 WordPress 主要版本上進行測試**。開發者可能不再對這個
外掛進行維護或提供技術支援，並可能會與更新版本的 WordPress 產生使用上的相容性問題。

![](https://s.w.org/plugins/geopattern-icon/geoswitch.svg)

# Geocode Switch

 由 [elialgranti](https://profiles.wordpress.org/elialgranti/) 開發

[下載](https://downloads.wordpress.org/plugin/geoswitch.1.1.4.zip)

 * [詳細資料](https://tw.wordpress.org/plugins/geoswitch/#description)
 * [使用者評論](https://tw.wordpress.org/plugins/geoswitch/#reviews)
 *  [安裝方式](https://tw.wordpress.org/plugins/geoswitch/#installation)
 * [開發資訊](https://tw.wordpress.org/plugins/geoswitch/#developers)

 [技術支援](https://wordpress.org/support/plugin/geoswitch/)

## 外掛說明

GeoSwitch is a plugin that allows you to change the content of your site based on
the location of your client’s IP.
 To geolocate users based on IP GeoSwitch supports
can user either the new the new GeoIP2 [MaxMind](https://www.maxmind.com) databases
or GeoIP2 Precision Service. MaxMind offers free and paid geolocation databases 
and the paid GeoIP2 Precision web service, the author of this plugin is not affiliated
with MaxMind in any way.

The main development of this plugin is in [github](https://github.com/elialgranti/geoswitch).

Please open a new [issue](https://github.com/elialgranti/geoswitch/issues) if you
find a bug in this plugin.

**This plugin uses the [MaxMind PHP library](http://maxmind.github.io/GeoIP2-php/)
which is released under the
 Apache License version 2.0

### Usage

= GeoSwitch Conditional Blocks =

GeoSwitch uses two shortcodes [geoswitch] and [geoswitch_case] to create conditional
blocks.
 The [geoswitch] shortcode is used to enclose one or more [geoswitch_case]
shortcodes with different conditions. The [geoswitch_case] shortcodes enclose content
that will be shown if the condition is true. The following example illustrates a
GeoSwitch conditional block that will show different contact information depending
on the user’s IP location:

    ```
    [geoswitch]
    [geoswitch_case country_code="AU" state_code="NSW"]New South Wales Office[/geoswitch_case]
    [geoswitch_case country_code="AU"]Australian Office[/geoswitch_case]
    [geoswitch_case]International Office[/geoswitch_case]
    [/geoswitch]
    ```

The above GeoSwitch block will display “New South Wales Office” to users in New 
South Wales within Australia,
 “Australian Office” to other Australian users and“
International Office” to any other user.

The [geoswitch_case] shortcode accepts the following attributes:

 * `country` – comma delimited list of country names
 * `country_code` – comma delimited list of country ISO codes
 * `state` – comma delimited list of of state names
 * `state_code` – comma delimited list of state ISO codes
 * `city` – comma delimited list of city names
 * `within`, `from` – This set of attributes is used to test for distances. Within
   is the distance in kilometers or miles and
    from is the centre point represented
   as “latitude,longitude” in degrees (example [geoswitch within=”10” from=”-33.7167,151.6”]).

The `[geoswitch_case]` shortcode matches only the attributes specified so:

    ```
    [geoswitch]
    [geoswitch_case city="paris"]You are in Paris![/geoswitch_case]
    [/geoswitch]
    ```

Will display “You are in Paris!” to any user with an IP location in a city named
Paris, e.g Paris, France or Paris, Texas, USA.
 A `[geoswitch_case]` shortcode without
any attributes always matches and can be used as the last condition in a conditional
block to show default content. Content between [geoswitch_case] blocks can contain
any markup including any other shortcodes, but conditional blocks should not be 
nested as this is not supported by WordPress. Content between the [geoswitch] and[
geoswitch_case] shortcodes should be whitespace but is usually ignored:

    ```
    [geoswitch] *DON’T WRITE HERE*
    [geoswitch_case]...[/geoswitch_case] *OR HERE*
    [geoswitch_case]...[/geoswitch_case]
    [/geoswitch]
    ```

#### Informational Shortcodes

In addition to the conditional block GeoSwitch offers the following shortcodes to
display user information:

 * `[geoswitch_ip]` – The user’s IP.
 * `[geoswitch_city]` – The user’s city name.
 * `[geoswitch_state]` – The user’s state name.
 * `[geoswitch_state_code]` – The user’s state ISO code.
 * `[geoswitch_country]` – The user’s country name.
 * `[geoswitch_country_code]` – The user’s country code.

If the IP of the user cannot be geo located these shortcodes return ‘?’

#### Debug Shortcodes

 * `geoswitch_setip` – Override the user IP. Affects shortcodes after this one.
   
   Usage:
 * [geoswitch_case ip=’10.0.0.10′]

For debugging whole websites the user IP can be set globally in the admin settings
for the plugin.

#### Filter Hooks

Geoswitch supports the following filter hooks:
 geoswitch_skip_ip_check Allows to
programatically skip the ip check. If the hook callback function returns true the
MaxMind libraries are not used to check the IP and the default content (if configured)
will be displayed. Usage:

    ```
    function skip_ip_callback( $skipCheck, $userIp ) {
        if ( some_custom_test($userIp) ) {
            $skipCheck = true;
        }
        return $skipChek;
    }

    add_filter( 'geoswitch_skip_ip_check', 'skip_ip_callback', 10, 2 );
    ```

## 安裝方式

#### Prerequisites

The Geoswitch plugin uses either MaxMind’s city database or webservice.

 * To use a local database for geocoding you’ll need either the free GeoLite2 city
   database
    (download from [here](http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz))
   or obtain a license from MaxMind for a GeoIP2 citydatabase. After obtaining the
   database uncompress it before installation.
 * To use the webservice you’ll need to obtain a user ID and license key for [GeoIP2 Precision Services](https://www.maxmind.com/en/geoip2-precision-services).

#### Installation

 1. Copy the Plugin directory to your wordpress plugins directory (usually wp-content/
    plugins)
 2. Optionally copy your MaxMind binary database to the database subdirectory inside
    the plugin root direcory (GeoSwitch/database).
     The database should be uncompressed.
 3. In the WordPress administration settings search for the GeoSwitch configuration
    page:
 4. Select the type of geocoding service to use (local database or webservice).
 5. Enter the name of the database or the user ID and license key depending on the 
    service you’ve selected.
 6. Set the units to use for distance calculations (kilometer or miles).

_Note: if you use the local database you should update it periodically._

## 常見問題集

  Installation Instructions

#### Prerequisites

The Geoswitch plugin uses either MaxMind’s city database or webservice.

 * To use a local database for geocoding you’ll need either the free GeoLite2 city
   database
    (download from [here](http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz))
   or obtain a license from MaxMind for a GeoIP2 citydatabase. After obtaining the
   database uncompress it before installation.
 * To use the webservice you’ll need to obtain a user ID and license key for [GeoIP2 Precision Services](https://www.maxmind.com/en/geoip2-precision-services).

#### Installation

 1. Copy the Plugin directory to your wordpress plugins directory (usually wp-content/
    plugins)
 2. Optionally copy your MaxMind binary database to the database subdirectory inside
    the plugin root direcory (GeoSwitch/database).
     The database should be uncompressed.
 3. In the WordPress administration settings search for the GeoSwitch configuration
    page:
 4. Select the type of geocoding service to use (local database or webservice).
 5. Enter the name of the database or the user ID and license key depending on the 
    service you’ve selected.
 6. Set the units to use for distance calculations (kilometer or miles).

_Note: if you use the local database you should update it periodically._

  Why do I see only question marks instead of my location?

Your IP was not found in the database. The most usual cause if that your browser
and server are behind a NAT and you are
 getting a private IP not a public one. 
For debugging purposes you can set the IP used by plug-in in the settings page.

  My location is wrong/partial. How come?

Geolocation using IP addresses is not entirely accurate. The geolocation relies 
on a static database of addresses and
 their approximate location, not exact location
like mobile GPS. To ensure maximum accuracy make sure you have the latest database.
Purchasing the non-lite version of the database from MaxMind or a license for their
service may also yield better information. I am not affiliated in any way with MaxMind,
so it is up to you to contact them and evaluate their offers for suitability to 
your purposes.

  How do I test other locations?

You can set the IP used in by the plug-in in the settings page and use Google to
search for IPs in the location you are interested
 in (i.e. IP in California) or
use the `geoswitch_setip` shortcode. Debug overrides for other setting are coming.

## 使用者評論

![](https://secure.gravatar.com/avatar/046c4b85efc0e9bb009599a261d614c35f0caf4d4dc9ca67505dd2ca0781828e?
s=60&d=retro&r=g)

### 󠀁[Excellent](https://wordpress.org/support/topic/excellent-5438/)󠁿

 [ilaos](https://profiles.wordpress.org/webstuffguy/) 2017 年 10 月 10 日

Provides everything you need to make geo-specific content for your website. I’ve
tried all the rest. This is the fastest, easiest and has the biggest impact.

![](https://secure.gravatar.com/avatar/bffef00d3ee520cec646ccfec1761b393c4582c67be1ec32f770e683d49186a9?
s=60&d=retro&r=g)

### 󠀁[Solved a nasty problem for me](https://wordpress.org/support/topic/solved-a-nasty-problem-for-me/)󠁿

 [ldblake](https://profiles.wordpress.org/ldblake/) 2017 年 9 月 23 日

I run a mixed public/private site where, for legal reasons, we need to restrict 
access to one province in Canada. (The law is different in other provinces). This
is the first plugin that even came close to solving the problem for us. I simply
wrapped my front end registration and login pages in the shortcodes and away it 
went! So long as it keeps working this nicely I’ll give it 4 stars. For the 5th 
star, add automatic data base updates.

 [ 閱讀全部 3 則使用者評論 ](https://wordpress.org/support/plugin/geoswitch/reviews/)

## 參與者及開發者

以下人員參與了開源軟體〈Geocode Switch〉的開發相關工作。

參與者

 *   [ elialgranti ](https://profiles.wordpress.org/elialgranti/)

[將〈Geocode Switch〉外掛本地化為台灣繁體中文版](https://translate.wordpress.org/projects/wp-plugins/geoswitch)

### 對開發相關資訊感興趣？

任何人均可[瀏覽程式碼](https://plugins.trac.wordpress.org/browser/geoswitch/)、查看
[SVN 存放庫](https://plugins.svn.wordpress.org/geoswitch/)，或透過 [RSS](https://plugins.trac.wordpress.org/log/geoswitch/?limit=100&mode=stop_on_copy&format=rss)
訂閱[開發記錄](https://plugins.trac.wordpress.org/log/geoswitch/)。

## 變更記錄

#### 1.1.4

 * Bug fix: Private and reserved IPs are not checked and no longer generate
 * Added filter hook `geoswitch_skip_ip_check` to bypass the ip check based on
    (
   Thanks to [Arūnas Liuiza](https://github.com/ideag) from [arunas.co](https://arunas.co/)
   for the BugFix and filter hook)

#### 1.1.3

Bug fix: when IP cannot be found plugin correctly evaluates all cases and uses default
empty case (if it exists), instead of not displaying anything.
 Implemented support
for comma separated values in geoswitch_case shortcode. Previous version mentioned
this feature in the Readme file, but it was not actually implemented.

#### 1.1.2

Error deploying to wordpress. Use version 1.1.3 instead.

#### 1.1.1

Added setting to for user IP for debugging purposes and updated MaxMind Libraries.

#### 1.1.0

 * Added support for MaxMind GeoIP2 Precision Service (thanks to [Paul Scarrone](https://github.com/ninjapanzer)
   
   and [carlcapozza](https://github.com/carlcapozza)).
 * Fixed bug with measurement units. Units were always considered kilometers.
 * Tested under WordPress 4.1.

#### 1.0.0

 * Initial release

## 中繼資料

 *  版本 **1.1.4**
 *  最後更新 **8 年前**
 *  啟用安裝數 **30+**
 *  WordPress 版本需求 ** 3.0 或更新版本 **
 *  已測試相容的 WordPress 版本 **4.9.29**
 *  語言
 * [English (US)](https://wordpress.org/plugins/geoswitch/)
 * 標籤:
 * [geocode](https://tw.wordpress.org/plugins/tags/geocode/)[geocode filter](https://tw.wordpress.org/plugins/tags/geocode-filter/)
   [geomarketing](https://tw.wordpress.org/plugins/tags/geomarketing/)[geotag](https://tw.wordpress.org/plugins/tags/geotag/)
 *  [進階檢視](https://tw.wordpress.org/plugins/geoswitch/advanced/)

## 評分

 4.7 星，滿分為 5 星

 *  [  2 個 5 星使用者評論     ](https://wordpress.org/support/plugin/geoswitch/reviews/?filter=5)
 *  [  1 個 4 星使用者評論     ](https://wordpress.org/support/plugin/geoswitch/reviews/?filter=4)
 *  [  0 個 3 星使用者評論     ](https://wordpress.org/support/plugin/geoswitch/reviews/?filter=3)
 *  [  0 個 2 星使用者評論     ](https://wordpress.org/support/plugin/geoswitch/reviews/?filter=2)
 *  [  0 個 1 星使用者評論     ](https://wordpress.org/support/plugin/geoswitch/reviews/?filter=1)

[Your review](https://wordpress.org/support/plugin/geoswitch/reviews/#new-post)

[查看全部使用者評論](https://wordpress.org/support/plugin/geoswitch/reviews/)

## 參與者

 *   [ elialgranti ](https://profiles.wordpress.org/elialgranti/)

## 技術支援

使用者可在技術支援論壇提出意見反應或使用問題。

 [檢視技術支援論壇](https://wordpress.org/support/plugin/geoswitch/)

## 贊助

想要支援這個外掛的發展嗎？

 [ 贊助這個外掛 ](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=C7QAD2M3L5T6E)