is_customize_preview()
云策文档标注
概述
is_customize_preview() 是一个 WordPress 条件函数,用于检测当前是否在自定义器(Customizer)中进行站点预览。它返回布尔值,帮助开发者在自定义器预览环境下执行特定代码。
关键要点
- 函数返回布尔值:true 表示在自定义器预览中,false 表示不在。
- 内部实现依赖于全局变量 $wp_customize 和 WP_Customize_Manager 实例的 is_preview() 方法。
- 常用于条件逻辑,以区分自定义器预览和正常前端环境。
代码示例
if ( is_customize_preview() ) {
// 在自定义器预览中执行特定操作
} else {
// 在其他环境下执行其他操作
}
原文内容
Whether the site is being previewed in the Customizer.
Source
function is_customize_preview() {
global $wp_customize;
return ( $wp_customize instanceof WP_Customize_Manager ) && $wp_customize->is_preview();
}
Changelog
| Version | Description |
|---|---|
| 4.0.0 | Introduced. |
Skip to note 2 content
Deepak Rajpal
Simple example to add something conditionally, if currently on customizer preview page:
if ( is_customize_preview() ) { // perform customizer specific action } else { // perform separate action }