我认为定制程序会为创建一个xhr请求来加载站点预览(请参见网络>xhr选项卡下的devtools)。在我的例子中,我想检查是否通过$\\u GET[\'foo\']变量设置了某个变量。我的函数必须调用一次而不是两次,否则它将覆盖我以前的状态。
我的解决方案是检查$\\u POST[\'wp\\u customize\']是否为空。如果为空,则表示我的函数不会被预览请求调用两次。
我使用以下挂钩/动作customize_loaded_components
示例:
function wpdocs_remove_widgets_panel( $components ) {
if( empty( $_POST[\'wp_customize\'] ) ) {
// Do stuff here that will not be loaded twice by the previewer
}
return $components;
}
add_filter( \'customize_loaded_components\', \'wpdocs_remove_widgets_panel\' );