wp admin – Don’t run code if in customizer or privew

Question

I have a function compiling my sass every time the page loads, IF it’s enabled in the theme. This of course gives a couple of seconds load time which is fine, it’s only used in development.

But I’m trying to prevent it from running if we are in the customiser seeing the site in the preview, but no matter which if sentence I give it, it stills compiles the css.

I have tried isset($wp_customize), is_admin() and is_customize_preview()

here a couple of examples of what I have tried.

global $wp_customize;
if ( !isset( $wp_customize ) ) {
    if ( get_theme_mod( 'enable_scss_compiler', false ) ) {
        if ( !is_customize_preview() ) {
             compile_theme_scss();
        }
    }
}

function compile_if_debug() {
    global $wp_customize;
    if ( !isset( $wp_customize ) ) {
        if ( get_theme_mod( 'enable_scss_compiler', false ) ) {
            if ( !is_customize_preview() ) {
                compile_theme_scss();
            }
        }
    }
}
add_action('init', 'compile_if_debug');

function compile_if_debug() {
    if ( !is_admin ) {
        if ( get_theme_mod( 'enable_scss_compiler', false ) ) {
            if ( !is_customize_preview() ) {
                compile_theme_scss();
            }
        }
    }
}
add_action('init', 'compile_if_debug');

But none of this works, and the sites still compiles when first entering the customizer which gives you that 10 seconds load time.

Does anyone have a solution to this problem.

0
bymem 6 months 2022-02-19T17:28:21-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse