skip to Main Content

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 );

3

Answers


  1. 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');
    
    Login or Signup to reply.
  2. 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' );
    
    Login or Signup to reply.
  3. 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/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search