• Hi,

    I’m trying to load a style file if a particular script is enqueued. My approach has been to test if the script is enqueued and if so enqueue the supportive css file. But the <link rel='stylesheet' id='init-style-css' ... is not in the head. Any ideas why?

    eg:

    function load_my_scripts() {
    	wp_register_script(
    		'init',
    		get_template_directory_uri() . '/assets/scripts/init.js',
    		false,
    		'1.0',
    		true
    		);
    
    	wp_register_style(
    		'init-style',
    		get_template_directory_uri() . '/assets/init-style.css',
    		false,
    		'0.1',
    		'all'
    		);
    
    	if ( is_home() ) {
    		wp_enqueue_script( 'init' );
    	}
    
    	if ( wp_script_is( 'init', 'enqueued' ) )
    		wp_enqueue_style( 'init-style' );
    }
    add_action( 'wp_enqueue_scripts', 'load_my_scripts' );

Viewing 3 replies - 1 through 3 (of 3 total)
  • Can you see if the script is loaded on your home page (is_home)?

    edit: I just tested your snippet and it works fine for me.

    So I wonder if this is a is_home vs is_front_page case?

    Thread Starter jlamerto

    (@jlamerto)

    Hmm… I can confirm that the enqueue script is being loaded just the style that ain’t.

    The problem seems to be the if ( wp_script_is( 'init', 'enqueued' ) ) which returns false.

    Changing it to:
    if ( ! wp_script_is( 'init', 'enqueued' ) )

    Give the correct result. But that makes me worry because it shouldn’t.

    Did you test it on the default theme?

    Does this work for you:

    if ( is_home() ) {
    	wp_enqueue_script( 'init' );
    	wp_enqueue_style( 'init-style' );
    }

    Sorry to ask, but you’re sure you aren’t enqueuing the init script somewhere else?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Help using wp_script_is() to conditionally load styles’ is closed to new replies.