Skip to content

Instantly share code, notes, and snippets.

@manishsongirkar
Created September 16, 2025 08:12
Show Gist options
  • Select an option

  • Save manishsongirkar/b39cb9f77e39dcce0915008253c77d52 to your computer and use it in GitHub Desktop.

Select an option

Save manishsongirkar/b39cb9f77e39dcce0915008253c77d52 to your computer and use it in GitHub Desktop.
Checks if the current page is being viewed in the Elementor editor.
<?php
/**
* Checks if the current page is being viewed in the Elementor editor.
*
* This function determines if the user is currently in the Elementor
* page builder's edit or preview mode. It also returns true if
* the user is in the WordPress admin area, as Elementor's editor
* is a part of the admin interface.
*
* @return bool True if in the Elementor editor or WordPress admin, false otherwise.
*/
function is_elementor_editor() {
if (
is_admin() || (
class_exists( '\Elementor\Plugin' ) &&
(
\Elementor\Plugin::$instance->preview->is_preview_mode()
|| \Elementor\Plugin::$instance->editor->is_edit_mode()
)
)
) {
return true;
}
return false;
}
# Usage
# Within function do not execute any frontend related changes in the editor.
if ( is_elementor_editor() ) {
return;
}
@manishsongirkar
Copy link
Author

/**
 * Disable Elementor related class from body tag.
 */

function custom_body_classes( $classes ) {
	if ( $disable_elementor ) {
		foreach ( array_keys( $classes, 'elementor-default', true ) as $key ) {
			unset( $classes[ $key ] );
		}

		$active_kit_id = get_option( 'elementor_active_kit' );
		foreach ( array_keys( $classes, sprintf( 'elementor-kit-%d', $active_kit_id ), true ) as $key ) {
			unset( $classes[ $key ] );
		}
	}

	return $classes;
}

add_filter( 'body_class', 'custom_body_classes', 999 );

@manishsongirkar
Copy link
Author

/**
 * Remove Elementor Post Styles.
 *
 * @return void
 */
function dequeue_elementor_post_styles() {

	if ( is_elementor_editor() ) {
		return;
	}

	$disable_elementor = true;
	$active_kit_handle = '';

	$active_kit_id = get_option( 'elementor_active_kit' );

	if ( $active_kit_id ) {
		$active_kit_handle = sprintf( 'elementor-post-%d', $active_kit_id );
	}

	$post_css_handle = sprintf( 'elementor-post-%d', get_the_ID() );

	/**
	 * Apply a filter to alter the state of `disable_elementor`
	 *
	 * This filter allows other plugins or themes to change the value of `$disable_elementor`,
	 * which can be used to enqueue/dequeue the Elementor Post Styles on certain Posts or conditions.
	 *
	 * @param bool $disable_elementor The current state of whether Elementor Post Styles Enqueue is disabled or not.
	 * @return bool Modified value of `$disable_elementor`.
	 */
	$disable_elementor = apply_filters( 'global_blocks_dequeue_elementor_post_styles', $disable_elementor, get_the_ID() );

	if ( ! is_admin() && $disable_elementor ) {

		if ( $active_kit_handle && wp_style_is( $active_kit_handle, 'enqueued' ) ) {
			wp_dequeue_style( $active_kit_handle );
		}

		if ( wp_style_is( $post_css_handle, 'enqueued' ) ) {
			wp_dequeue_style( $post_css_handle );
		}
	}
}

add_action( 'elementor/css-file/post/enqueue', 'dequeue_elementor_post_styles' );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment