
內容簡介
WordPress 後台有全站時區設定,前台顯示的所有日期和時間通常會顯示在這個時區。
這個外掛可使用瀏覽器時區顯示前端的日期和時間。透過覆寫 WordPress 的 'timezone_string' 和 'gmt_offset' 選項,來符合使用者的時區在特定請求的持續時間的前端介面。
瀏覽器時區偵測
瀏覽器時區是使用以下兩種方法之一偵測的
jsTimezoneDetect(預設)
HTML5 Geolocation API 和 Google Timezone API
jsTimezoneDetect(預設)
這是預設的時區偵測機制。請參閱腳本的主頁
以取得相容性和其他注意事項
HTML5 Geolocation API
使用navigator.geolocation.getCurrentPosition來偵測目前的地理位置。之後將偵測到的地理位置傳送至Google Timezone API
此 API 會返回類似於「Asia/Kolkata」的時區 ID。
此方法需要您
在外掛選項頁面上啟用 "Google 時區 API" 選項
指定有效的Google API Key
此金鑰應該是瀏覽器專屬金鑰
"接受的 HTTP 引薦者" 應該包括使用主機 WordPress 網站的域名,或留白以允許所有引薦者
如果未達成使用 Google 時區 API 所需的必要選項,則偵測機制將回復到預設值。
文章時區偵測
如果瀏覽器的時區 ID 未被成功偵測,則其他所有操作都會像未安裝此外掛一樣。
如果偵測到瀏覽器的時區 ID,則會執行以下操作
會根據在外掛選項頁面上指定的 cookie 選項,在瀏覽器中設置 cookie
如果具有相同 ID 的 cookie 已存在,則不會採取任何行動
如果以前不存在 cookie 或存在具有不同時區 ID 的 cookie,則會觸發 window.reload 以刷新頁面,以便更新頁面上的日期和時間
EventON 拋鉤
此外掛提供一個功能,可掛鉤到 EventON 外掛。啟用後,所有 EventON 的日期和時間都會更新以反映瀏覽器的時區。
簡碼
此外掛定義以下簡碼
[wp_user_timezone_id] => 返回瀏覽器的時區 ID,如果不可用則返回 null。
[wp_user_timezone_offset] => 返回瀏覽器的時區的 GMT 偏移量(小時),如果不可用則返回 null。
選項
此外掛定義以下選項以檢索在站點層級定義的原始 timezone_string 和 gmt_offset
get_option ('site_timezone_string') => 返回站點的原始 timezone_string 選項
get_opt
外掛標籤
開發者團隊
原文外掛簡介
WordPress has a site-level timezone setting configured from the admin back-end. All dates and times displayed on the front-end are typically displayed in this timezone.
This plugin enables the display of dates/times in the front-end using the browser’s timezone. This is done by overriding the WordPress’s ‘timezone_string’ and ‘gmt_offset’ options to match the user’s timezone for the duration of a particular request
ONLY on the front-end interface.
Browser Timezone Detection
The browser’s timezone is detected using one of the following two methods
jsTimezoneDetect (default)
HTML5 Geolocation API and Google Timezone API
jsTimezoneDetect (default)
This is the default timezone detection mechanism. Please refer to the script home page
for the compatibility and other notes
HTML5 Geolocation API
Detects the current geo-location using navigator.geolocation.getCurrentPosition. The location thus detected will be sent to the Google Timezone API
which returns the timezone id – similar to “Asia/Kolkata”.
This method requires you to
Enable the “Google Timezone API” option on the plugin options page
Specify a valid Google API Key
The key should be a browser key
The “Accepted HTTP Referrers” should include the domain name used by the hosting wordpress site or left blank to allow all referrers
If the required options for the usage of Google Timezone API are not met, the detection mechanism falls back to the default one.
Post Timezone Detection
If the browser’s timezone id is not detected successfully, everything else behaves as if this plugin is not installed.
If the browser’s timezone id is detected, the following actions take place
A cookie will be set in the browser, based on the cookie options specified on the plugin options page
If a cookie already exists previously with the same id, no action is taken
If a cookie doesn’t already exist previously or exists with a different timezone id, then a window.reload is triggered to refresh the page so that the dates/times on the page are updated
EventON Hook
This plugin provides a feature to hook into the EventON plugin. When enabled, all EventON dates/times are updated to reflect the browser’s timezone.
Shortcodes
This plugin defines the below shortcodes
[wp_user_timezone_id] => Returns the timezone id of the browser or null if not available.
[wp_user_timezone_offset] => Returns the timezone’s gmt offset in hours of the browser or null if not available.
Options
This plugin defines the below options to retrieve the original timezone_string and gmt_offset defined at the site level
get_option( ‘site_timezone_string’ ) => Returns the original timezone_string option for the site
get_option( ‘site_gmt_offset’ ) => Returns the original gmt_offset option for the site
Actions/Hooks
This plugin defines the below filters/hooks
apply_filters( ‘wp_user_timezone_id’, $tz_id ) => Allows you to override the timezone string determined by the plugin. This filter can be used to apply user’s timezone preference. If any plugin or wordpress core implements a feature to accept and store user’s preferred timezone, the ‘wp_user_timezone_id’ can be used to apply that timezone value to the front-end. Reference: Add user-level timezone setting).
Admin Backend Dates/Times
Important: The admin back-end is not modified by this plugin. All dates/times displayed in the back-end are still in the original timezone specified by the WordPress General Settings.
