如何更改WordPress的标题x框架选项SAMEORIGIN到ALLOWALL或特定的网址

beq87vna  于 2023-03-07  发布在  WordPress
关注(0)|答案(1)|浏览(100)

enter image description here

function send_frame_options_header() {
  header( 'X-Frame-Options: SAMEORIGIN' );
}

如何通过过滤器或插件更改此功能?
我需要更改“全部允许”
是否可以通过插件实现?

myzjeezk

myzjeezk1#

这个send_frame_options_header()函数在两个不同的钩子上作为WordPress动作处理程序被调用。

add_action( 'admin_init', 'send_frame_options_header', 10, 0 );

以及

add_action( 'login_init', 'send_frame_options_header', 10, 0 );

如果你为这些钩子设置了自己的action处理程序,并给予它们比10更高的优先级,你可以通过调用header_remove()来覆盖它们所做的事情。

function fasal_basha_send_frame_options_header () {
  header_remove( 'X-Frame-Options' );
}
add_action( 'admin_init', 'fasal_basha_send_frame_options_header ', 11, 0 );
add_action( 'login_init', 'fasal_basha_send_frame_options_header ', 11, 0 );

注意优先级11,你希望钩子函数在核心函数之后运行。
X-Frame-Options header不再有Allow变量。如果你想指定哪些域的iframe代码可以嵌入你的页面,你需要使用内容安全策略frame-ancestors指令。
在你的钩子函数中添加一行代码,这样你的页面就可以嵌入你自己的站点和www.example.com

function fasal_basha_send_frame_options_header () {
  header_remove( 'X-Frame-Options' );
  header( 'Content-Security-Policy: frame-ancestors 'self' https://www.example.com;' );
}

相关问题