Skip to content

Instantly share code, notes, and snippets.

@gatespace
Last active February 8, 2017 09:33
Show Gist options
  • Save gatespace/60736fc32e3fb483b17ed25d3798aec6 to your computer and use it in GitHub Desktop.
Save gatespace/60736fc32e3fb483b17ed25d3798aec6 to your computer and use it in GitHub Desktop.
WordPress Transients API を使うときの覚書 ref: http://qiita.com/gatespace/items/90fad367f9ad0626e720
# Set transient.
$ wp transient set sample_key "test data" 3600
Success: Transient added.
# Get transient.
$ wp transient get sample_key
test data
# Delete transient.
$ wp transient delete sample_key
Success: Transient deleted.
# Delete expired transients.
$ wp transient delete --expired
Success: 12 expired transients deleted from the database.
# Delete all transients.
$ wp transient delete --all
Success: 14 transients deleted from the database.
<?php
// Transient データを取得する
if ( WP_DEBUG or false === ( $value = get_transient( 'sample_key' ) ) ) {
// Transient が存在しない場合 or デバッグモードのときにこの部分のコードが実行
$value = ... // ここで $value の値を設定し直す
// $value を Transient へ保存。12時間後に無効にする。
set_transient( 'sample_key', $value, 12 * HOUR_IN_SECONDS );
}
echo $value;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment