聊聊WordPress怎么通過header給頁面發送原生HTTP頭

本篇文章給大家帶來了關于wordpress的相關知識,其中主要跟大家聊一聊WordPress怎么通過header給頁面發送原生http頭,感興趣的朋友下面一起來看一下吧,希望對大家有幫助。

聊聊WordPress怎么通過header給頁面發送原生HTTP頭

在PHP中,我們可以用 header 函數來發送原生 HTTP 頭,但在WordPress中怎么發送HTTP 頭呢?

方法一:自己寫代碼

將下面的代碼放到當前主題的functions.php中即可:

/** *??在用戶登陸的情況下,給前臺所有頁面添加不緩存的?Cache-Control?頭?? */ function?ludou_http_headers()?{ ??//?判斷用戶是否登陸,并且是在非后臺(前臺)頁面 ??if(is_user_logged_in()?&&?!is_admin())?{ ????//?php的header函數發送HTTP?頭 ????header('Cache-Control:?no-store,?no-cache,?must-revalidate,?max-age=0'); ??} } /*?WordPress?hook *?第一個參數值?wp?是action動作名稱,文檔:https://codex.wordpress.org/Plugin_API/Action_Reference/wp *?第二個參數值?ludou_http_headers?是上面的函數名稱,自己取名 */ add_action(?'wp',?'ludou_http_headers'?);

方法二:用插件

HTTP Headers:https://wordpress.org/plugins/http-headers/HTTP headers to improve web site security:https://wordpress.org/plugins/http-security/

推薦學習:《WordPress教程

? 版權聲明
THE END
喜歡就支持一下吧
點贊7 分享