How to get this WP script_loader_tag working?

314 Views Asked by At

I've read about two dozen posts on here, but can't work out why this isn't working...

I'm enqueuing two scripts, then using a script_loader_tag to add type="module" to each tag. Scripts get loaded, but no type="module" appears.

functions.php code below, site is radhr.org, many thanks for any help...

// Load RoughNotation page title animations

function notation_page_js() {
    if( !is_page('2') ) {
        wp_enqueue_script( 'js-file', get_stylesheet_directory_uri() . '/js/rn-page.js');
    }
}

add_action('wp_enqueue_scripts', 'notation_page_js');

// Load RoughNotation home hero animation

function notation_home_js() {
    if( is_page('2') ) {
        wp_enqueue_script( 'js-file', get_stylesheet_directory_uri() . '/js/rn-home.js');
    }
}

add_action('wp_enqueue_scripts', 'notation_home_js');

// Add module type to RoughNotation script tags

function add_type_attribute($tag, $handle, $src) {
        // if not your script, do nothing and return original $tag
        if ( 'notation_page_js || notation_home_js' === $handle ) {
                // change the script tag by adding type="module" and return it.
                $tag = '<script type="module" src="'. esc_url($src) .'"></script>';
        }
        return $tag;
}

add_filter('script_loader_tag', 'add_type_attribute' , 10, 3);
1

There are 1 best solutions below

0
On

I believe that you should change

if ('notation_page_js || notation_home_js' === $handle)

by

if ('js-file' === $handle)