聊聊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 分享