JiveDig
2/18/2019 - 4:50 PM

What happens when you try to enqueue a script in WordPress with the same handle or file name and location?

What happens when you try to enqueue a script in WordPress with the same handle or file name and location?

<?php

$dir = get_stylesheet_directory_uri();

// Different scripts: Both are loaded.
wp_enqueue_script( 'test-one', $dir . '/assets/js/test-one.js', array(), '1.0.0', true );
wp_enqueue_script( 'test-two', $dir . '/assets/js/test-two.js', array(), '1.0.0', true );

// Identical scripts: 1 is loaded.
wp_enqueue_script( 'test-one', $dir . '/assets/js/test.js', array(), '1.0.0', true );
wp_enqueue_script( 'test-one', $dir . '/assets/js/test.js', array(), '1.0.0', true );

// Different handles, same file name/location: Both are loaded.
wp_enqueue_script( 'test-one', $dir . '/assets/js/test.js', array(), '1.0.0', true );
wp_enqueue_script( 'test-two', $dir . '/assets/js/test.js', array(), '1.0.0', true );

// Same handles, different file name/location: The first script is loaded (test.js).
wp_enqueue_script( 'test-one', $dir . '/assets/js/test.js', array(), '1.0.0', true );
wp_enqueue_script( 'test-one', $dir . '/assets/js/test-two.js', array(), '1.0.0', true );

// Same handles, same file name/location, different versions: The first script is loaded (1.0.0).
wp_enqueue_script( 'test-one', $dir . '/assets/js/test.js', array(), '1.0.0', true );
wp_enqueue_script( 'test-one', $dir . '/assets/js/test.js', array(), '2.0.0', true );

// Same handles, different file name/location, different versions: The first script is loaded (1.0.0).
wp_enqueue_script( 'test-one', $dir . '/assets/js/test.js', array(), '1.0.0', true );
wp_enqueue_script( 'test-one', $dir . '/assets/js/test-two.js', array(), '2.0.0', true );

// Register first, then enqueue, same handle, same file names, different versions: The first script is loaded (1.0.0).
wp_register_script( 'test-one', $dir . '/assets/js/test.js', array(), '1.0.0', true );
wp_register_script( 'test-one', $dir . '/assets/js/test.js', array(), '2.0.0', true );
wp_enqueue_script( 'test-one' );