Register & Enqueue Script Generator

Generate wp_enqueue_script() code with dependencies, a cache-busting version, footer loading, conditional context and wp_localize_script data.

Live output

Enable JavaScript to customise; default output below.

The name other code uses to depend on, dequeue or localize this script. Prefix it.

Relative to the plugin or theme root, not a full URL.

Load in

Picks the hook: wp_enqueue_scripts, admin_enqueue_scripts, enqueue_block_editor_assets or login_enqueue_scripts.

Source

Decides whether the URL is built with plugins_url, get_template_directory_uri or get_stylesheet_directory_uri.

Live preview enqueue-script.php
<?php
/**
 * Enqueues the "my-plugin-frontend" script.
 */
function my_plugin_enqueue_scripts() {
	$src  = plugins_url( 'assets/js/frontend.js', __FILE__ );
	$file = plugin_dir_path( __FILE__ ) . 'assets/js/frontend.js';

	// filemtime changes whenever the file does, so browsers never serve a stale copy.
	$version = file_exists( $file ) ? filemtime( $file ) : false;

	wp_enqueue_script(
		'my-plugin-frontend',
		$src,
		array(),
		$version,
		array(
			'in_footer' => true,
			'strategy'  => 'defer',
		)
	);
}
add_action( 'wp_enqueue_scripts', 'my_plugin_enqueue_scripts' );

Output is valid and updates as you type.

Point at the script file, say where it loads and what it depends on, and the generator writes the wp_enqueue_script() call with a cache-busting version and the right hook.

How to use

  1. Name the handle. Other code uses it to depend on your script, dequeue it or pass data to it, so prefix it.
  2. Give the path relative to your plugin or theme root. The generator builds the URL and the filesystem path from it, because the version needs both.
  3. List dependencies by handle. jquery for jQuery, wp-element for React in the block editor.
  4. Leave the version on filemtime unless you have a build step that fingerprints filenames.
  5. Turn on passing data if the script needs a REST URL or a nonce, and pick a conditional if the file only matters on some pages.

Example

An admin script that needs jQuery and the i18n package, versioned from a constant, with a REST URL and nonce handed to it:

wp_enqueue_script(
	'acme-admin',
	$src,
	array_map( 'trim', explode( ',', 'jquery, wp-i18n' ) ),
	$version,
	array(
		'in_footer' => true,
		'strategy'  => 'defer',
	)
);

wp_localize_script(
	'acme-admin',
	'acmeAdmin',
	array(
		'restUrl' => esc_url_raw( rest_url() ),
		'nonce'   => wp_create_nonce( 'wp_rest' ),
	)
);

filemtime() is what makes the browser pick up a change: the version string moves whenever the file does, so there is no stale copy to clear.

Pitfalls

  • Hardcoding a version string means editing it on every deploy, and forgetting once means users run old code until they clear their cache.
  • filemtime() on a missing file throws a warning, which is why the generated code checks file_exists() first.
  • An unprefixed handle can collide with another plugin. WordPress silently keeps the first registration and ignores yours.
  • Enqueueing outside a hook, directly at file load, runs too early and the script never reaches the page.
  • admin_enqueue_scripts fires on every admin screen. Without a screen check you ship your file to the whole dashboard.
  • async does not preserve execution order, so a script with dependencies can run before them. Use defer unless the file is genuinely standalone.
  • wp_localize_script() must run after the script is registered or enqueued, never before, and it outputs the data as a global.
  • Block editor assets belong on enqueue_block_editor_assets, not admin_enqueue_scripts, or they load on screens with no editor.

Compatibility

wp_enqueue_script() has taken this argument shape since WordPress 2.6. The array( 'in_footer', 'strategy' ) form needs WordPress 6.3 or later; on older versions pass the boolean instead, which the tool writes when the strategy is set to none. Generated code targets PHP 7.0 and up, and the tool runs in your browser.

Frequently asked questions

Register or enqueue?
Enqueue when you always want it on that page. Register when something else decides later, such as a shortcode enqueuing it only when used.
Where do dependencies come from?
Any handle WordPress or another plugin has registered. Core ships a long list, including jquery, wp-i18n and wp-element.
Why does my script 404?
The path is relative to the file the code lives in, because plugins_url() is given __FILE__. Moving the snippet to a different folder changes the URL it builds.
Should everything go in the footer?
Almost always. A script in the head blocks rendering until it downloads, and the footer plus defer is what modern pages want.
How do I stop it loading on every page?
Use the conditional. It writes the early return, so the file is never enqueued where it does nothing.

From the people who built this tool

WP Adminify

The WordPress admin, rebuilt: a dashboard worth looking at, menu and column control, a real file manager and the login page your client sees.

See WP Adminify Free version on WordPress.org

Weekly drops

New tools, when there are new tools

One email when something worth using ships. No schedule to fill, so no filler.

Your address goes nowhere else, and one click unsubscribes.