### [聊聊WordPress怎么通过header给页面发送原生HTTP头](https://www.prosaas.cn/article/7604) **Published:** 2023-02-22T03:49:58 **Author:** 5201314 **Excerpt:** 本篇文章给大家带来了关于WordPress的相关知识,其中主要跟大家聊一聊WordPress怎么通过heade… 本篇文章给大家带来了关于WordPress的相关知识,其中主要跟大家聊一聊WordPress怎么通过header给页面发送原生HTTP头,感兴趣的朋友下面一起来看一下吧,希望对大家有帮助。 ![](//zhiqipro.oss-cn-beijing.aliyuncs.com/2023/12/20231212130303659.jpg) 在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教程》 以上就是聊聊WordPress怎么通过header给页面发送原生HTTP头的详细内容,更多请关注知企PROSAAS其它相关文章! **Categories:** WordPress ---