wordpress 检查元素或可视化编辑器视图是否处于活动状态

toe95027  于 9个月前  发布在  WordPress
关注(0)|答案(3)|浏览(167)

如何检查WordPress页面当前是“作为常规页面”查看还是“在Elementor可视化编辑器中”查看?

我写了一个插件,当一个特定的简码,如果在一个页面上出现重定向用户。它的工作原理就像一个魅力,但不幸的是,当一个页面重定向客户端时,Elementor可视化编辑器死亡。我希望插件重定向时,Elementor编辑器不活跃。
我的第一个想法是检查URL是否包含action=elementor,就像Elementor编辑器处于活动状态时一样,并执行以下操作:

global $wp;
if ( strpos(home_url( $wp->request ), 'action=elementor') !== false ) {
    // don't redirect
}

字符串
但这并不起作用,因为home_url( $wp->request )只返回页面的永久链接,而不是实际调用的URL。

b5lpy0ml

b5lpy0ml1#

好吧,没关系...这是个窍门:

if ( strpos($_SERVER['REQUEST_URI'], 'elementor') !== false ) {
    // don't redirect
}

字符串

c2e8gylq

c2e8gylq2#

您可以使用此

if ( \Elementor\Plugin::$instance->preview->is_preview_mode() ) {
    //do something
  } else {
   //do something
  }

字符串

tuwxkamq

tuwxkamq3#

你可以试试下面两种方法,这才是正确的检查方法。

// Check if Elementor's editor mode is active
if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
    // The code here will run when the Elementor editor mode is active
} else {
    // The code here will run when the Elementor editor mode is not active
}

// Check if Elementor's preview mode is active
if ( \Elementor\Plugin::$instance->preview->is_preview_mode() ) {
    // The code here will run when the Elementor preview mode is active
} else {
    // The code here will run when the Elementor preview mode is not active
}

字符串

相关问题