skip to Main Content

How do I check whether a WordPress page is currently viewed “as a regular page” or “in the Elementor visual editor”?

I’ve written a plugin that redirects users when a specific shortcode if present on a page. It works like a charm, but unfortunately the Elementor visual editor dies when a page redirects the client. I want the plugin to redirect only when the Elementor editor isn’t active.

My first idea was to check if the URL contains action=elementor, as it does when Elementor editor is active, and do something like this:

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

but this does not work, as home_url( $wp->request ) only returns the permalink of the page but not the actually called URL.

2

Answers


  1. Chosen as BEST ANSWER

    Okay nevermind... This does the trick:

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

  2. You can use this

      if ( ElementorPlugin::$instance->preview->is_preview_mode() ) {
        //do something
      } else {
       //do something
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search