1 回答

TA貢獻1853條經驗 獲得超9個贊
為此,您可以使用 Wordpress 瞬態 API ( https://codex.wordpress.org/Transients_API )。瞬態是一段具有超時的臨時鍵值數據。在每次頁面加載時,您測試您的瞬態是否存在。如果不是,請將其設置為 x 分鐘過期時間(或您想要的任何過期時間)并執行您的代碼。如果另一個用戶在過期時間內訪問該頁面,瞬態仍然存在,代碼將不會執行:
define('TRANSIENT_MY_INTERVAL'); // 5 minute interval for expiration
add_action('wp_loaded', 'do_this_regularly');
function do_this_regularly()
{
$trans_timeout = 5 * MINUTE_IN_SECONDS; // define your timeout here
$trans_lock = get_transient('my_transient_lock');
if (! $trans_lock) { // will be false if expired or not set
set_transient('my_transient_lock', true, $trans_timeout); // set the lock
// <- perform your every x minutes action here!
}
}
此代碼僅在人們實際訪問該頁面時才會執行,就像使用 WP cron ( https://developer.wordpress.org/plugins/cron )一樣。這也可以用于相同的確切目的 - 只是使用哪個的偏好問題。
- 1 回答
- 0 關注
- 104 瀏覽
添加回答
舉報