25% off ProSNIPC25OFF

Elementor: Add Body Class on Elementor-Built Pages

Appends a has-elementor-page CSS class to the body tag on any singular page built with Elementor, enabling targeted stylesheet overrides.

PHPby SnipCraft
php
<?php
/**
 * Add a CSS body class on pages built with Elementor.
 * Uses Elementor's Documents Manager to check the builder status per page.
 * Safe no-op when Elementor is not installed.
 */
add_action( 'plugins_loaded', function() {
    if ( ! defined( 'ELEMENTOR_VERSION' ) ) {
        return;
    }
    add_filter( 'body_class', function( $classes ) {
        if ( ! is_singular() ) {
            return $classes;
        }
        $post_id = (int) get_the_ID();
        if ( ! $post_id ) {
            return $classes;
        }
        $document = \Elementor\Plugin::$instance->documents->get( $post_id );
        if ( $document && $document->is_built_with_elementor() ) {
            $classes[] = 'has-elementor-page';
        }
        return $classes;
    } );
} );
#body-class#css#elementor#frontend#theming