[WordPress] 外掛分享: Cookie Tasting

首頁外掛目錄 › Cookie Tasting
WordPress 外掛 Cookie Tasting 的封面圖片
10+
安裝啟用
★★★★★
5/5 分(1 則評價)
1969 天前
最後更新
問題解決
WordPress 5.0+ PHP 7.0+ v1.0.11 上架:2019-03-13

內容簡介

此外掛會在使用者登入時設置使用者 Cookie。
您可以將 Cookie 用作資料儲存庫,
因此您可以將其用作 UI 資源。

可見性

此外掛會將 class 添加到 html 元素。

ct-logged-in:目前使用者已登入。
ct-not-logged-in:目前使用者為匿名使用者。

您可以使用 CSS 控制元素的顯示和隱藏。

.some-element{
display: none;
}
.ct-logged-in .some-element{
display: block;
}
從 JavaScript 中使用

您可以使用全域物件 CookieTasting 進行效用的呼叫。

CookieTasting.userName():傳回使用者名稱。如果未登入,則傳回「Guest」。
CookieTasting.lastUpdated():傳回最後登入檢查的時間戳記。如果等於 0,表示使用者為匿名使用者。

此外,此外掛會週期性地檢查登入狀態。
您可以使用 jQuery 來處理這個檢查程序。

jQuery( document ).on( 'cookie.tasting', function( event, response ) {
if ( response.login ) {
// 使用者已登入。
// 如果您使用 React...
setAttributes({ name: CookieTasting.userName() })
} else {
// 使用者尚未登入。
}
} );

如果您使用 React 或其他類似程式庫,請使用 setState() 更新狀態。

在執行操作前進行檢查

如果您管理快取的 WordPress 和自訂主題,
使用 JavaScript 實現動態 UI 元件是一個不錯的選擇。

在重要動作之前,您可以檢查使用者的憑證。

// 按鈕的點擊操作。
$('.read-more').click( function( e ) {
e.preventDefault();
// 在執行某些操作之前檢查 Cookie。
CookieTasting.testBefore().then( function( response ) {
// 現在使用者已經取得最新的資訊。
// 載入高級內容。
loadPremiumContents();
}).catch( function( response ) {
// 此使用者尚未登入。
// 重新導向到登入頁面。
window.locaion.href = '/wp-login.php';
} );
} );

請務必將 cookie-tasting-heartbeat 的相依套件加入您的程式中。

處理 UUID

此外掛預設會為每個使用者設置 UUID。這將會是...

對於每個已經登入的使用者,將是唯一的並會儲存在使用者 meta 中。
對於匿名使用者,也會保留。

因此,您可以將其用於 Google Analytics 的 使用者 ID 檢視。

const uuid = CookieTasting.get( 'uuid' );
// 用於 Google Analytics。
ga( 'set', "userId", uid );

外掛標籤

開發者團隊

⬇ 下載最新版 (v1.0.11) 或搜尋安裝

① 下載 ZIP → 後台「外掛 › 安裝外掛 › 上傳外掛」
② 後台搜尋「Cookie Tasting」→ 直接安裝(推薦)
📦 歷史版本下載

原文外掛簡介

This plugin sets user cookie when user is logged in.
You can use cookie as data store,
so you can use it as UI resource.
Visibility
This plugin adds class to html element.

ct-logged-in The current user is logged in.
ct-not-logged-in The current user is anonymous.

You can control elements visibility with CSS.
.some-element{
display: none;
}
.ct-logged-in .some-element{
display: block;
}
From JavaScript
You can use Global Object CookieTasting for utility.

CookieTasting.userName() Returns user name. If not logged in, returns ‘Guest’.
CookieTasting.lastUpdated() Returns timestamp of last log in check. If this equals 0, it means that user is anonymous.

Besides that, this plugin checks periodically log-in status.
You can handle it with jQuery.
jQuery( document ).on( 'cookie.tasting', function( event, response ) {
if ( response.login ) {
// User is logged in.
// If you use React...
setAttributes({ name: CookieTasting.userName() })
} else {
// User is not logged in.
}
} );
If you use react or something, updated the status with setState().
Check Before Action
If you manage cached WordPress and customizing your own theme,
It’s a good idea to implement dynamic UI components with JavaScript.
You can check user’s credential just before important actions.
// Click action for button.
$('.read-more').click( function( e ) {
e.preventDefault();
// Check cookie before do something.
CookieTasting.testBefore().then( function( response ) {
// Now user has fresh information.
// Load premium contents.
loadPremiumContents();
}).catch( function( response ) {
// This user is not logged in.
// Redirect them to login page.
window.locaion.href = '/wp-login.php';
} );
} );
Plese remember adding dependency for cookie-tasting-heartbeat to your script.
Handle UUID
By default, this plugin set UUID for each user. This will be…

Unique for each logged in user and will be saved as user_meta.
Also kept for anonymous user.

So you can use it for Google Analytic’s User ID View.
const uuid = CookieTasting.get( 'uuid' );
// For Google Analytics.
ga( 'set', "userId", uid );

延伸相關外掛

文章
Filter
Apply Filters
Mastodon