Home > Back-end >  How to enqueue module script?
How to enqueue module script?

Time:01-06

I tried to enqueue a module script in WordPress. But not enquiring the script on wordpress.

I have tried: wp_enqueue_script( 'handle', 'https://unpkg.com/@google/model-viewer/dist/model-viewer.min.js', [], 1.0.0 );

CodePudding user response:

you can use it

wp_register_script('handle', 'https://unpkg.com/@google/model-viewer/dist/model-viewer.min.js', ['jquery'], '1.0.0', true);
wp_enqueue_script('handle');
wp_scripts()->add_data('handle', 'type', 'module');

CodePudding user response:

you must register script first, then enqueuing.

function register_and_enqueue_script() {
    //register script
    wp_register_script( 'handle', 'https://unpkg.com/@google/model-viewer/dist/model-viewer.min.js', [], 1.0.0 );
    
    // enqueuing script 
    wp_enqueue_script( 'handle' );
}
add_action( 'wp_enqueue_scripts', 'register_and_enqueue_script' );

CodePudding user response:

function my_enqueue_scripts() {
    wp_register_script('my-module-script', 
                    get_template_directory_uri() .'/myscript.js',   //
                    array ('jquery', 'jquery-ui'),                  //depends on these, however, they are registered by core already, so no need to enqueue them.
                    '1.0.0', 
                     false
                    );

    wp_enqueue_script( 'my-module-script');
}

add_action( 'wp_enqueue_scripts', 'my_enqueue_scripts' );

For Details: https://developer.wordpress.org/reference/functions/wp_enqueue_script/

  • Related