Independent Editorial DeskWordPress Releases, Builds, and Operations
Back to Archive
Implementation Notes

WordPress wp_script_add_data Example: Add defer and async Safely

Use wp_script_add_data() to attach native WordPress script loading strategy metadata such as defer or async to existing handles.

Published

May 1, 2026

Reading Time

2 min read

Updated

May 1, 2026

Abstract asset strategy panel representing WordPress script loading metadata.
Build PatternImplementation Notes

Implementation Notes

Extension points, code paths, and implementation choices that should survive contact with production.

Best For

WordPress developers, agencies, and technical teams building custom plugin or theme functionality with cleaner operational defaults.

Primary Topics

Implementation Notes

Editorial Focus

Build Pattern: Extension points, code paths, and implementation choices that should survive contact with production. Updated on May 1, 2026.

Full Report

Last reviewed: May 1, 2026

Front-end performance work often stalls because scripts are enqueued correctly but still loaded with the wrong execution strategy. wp_script_add_data() is the native helper for attaching metadata to an already registered script, including modern loading strategies such as defer and async.

This guide shows how to use wp_script_add_data() safely so WordPress controls script behavior without brittle manual tag rewriting.

Register or enqueue the handle first

<?php
add_action( 'wp_enqueue_scripts', 'vulnwp_enqueue_filters_script' );

function vulnwp_enqueue_filters_script() {
	wp_enqueue_script(
		'vulnwp-filters',
		get_stylesheet_directory_uri() . '/assets/js/filters.js',
		array(),
		null,
		true
	);

	wp_script_add_data( 'vulnwp-filters', 'strategy', 'defer' );
}

The WordPress reference is explicit here: the helper works only if the script handle already exists.

Use strategy metadata for loading behavior, not for config

wp_script_add_data() is for metadata about the script tag. If the code needs runtime configuration, use wp_add_inline_script() instead of forcing config into the wrong helper.

Defer and async are not interchangeable

defer preserves execution order relative to other deferred scripts. async does not. Choose based on the dependency graph, not on whichever sounds faster.

Keep the handle stable across theme and plugin layers

If another part of the stack already registers the script, add metadata to the same handle instead of duplicating the asset with a slightly different name.

Production checklist

  • Attach metadata only after the script is registered or enqueued.
  • Use strategy for loading behavior, not config objects.
  • Pick defer or async based on real dependency order.
  • Retest any script that depends on DOM readiness or localized globals.
  • Keep script handles consistent across theme and plugin code.
  • Verify the rendered script tag in production HTML, not only in PHP.

Common mistakes

  • Calling the helper before the handle exists. The metadata never attaches.
  • Using async on ordered dependency chains. Execution can break quietly.
  • Trying to pass config through script metadata. That belongs in inline data helpers.
  • Duplicating the same script under multiple handles. Asset control becomes inconsistent.
  • Assuming performance wins without front-end verification. Script timing changes can create regressions.

Related reading

If the asset itself still needs proper registration and dependencies, pair this with the wp_enqueue_script guide. If the script also needs runtime data from PHP, continue with the wp_localize_script article.

References and further reading

Popular Guides

Popular WordPress guides to read next.

These articles connect recurring production concerns: implementation details, updates, troubleshooting, recovery paths, and operational cleanup.

Continue Reading

More from the archive.

Diagnostic dashboard scene representing a WordPress Site Health review before major updates.
01Build Pattern
Implementation Notes

Build Pattern

Extension points, code paths, and implementation choices that should survive contact with production.

May 21, 2026 · 3 min read

WordPress Site Health Check Before Major Updates: What to Review First

A pre-update WordPress Site Health checklist covering loopbacks, connectivity, debug settings, and environment readiness.

Structured data and route review scene representing permalink validation after a WordPress migration.
02Build Pattern
Implementation Notes

Build Pattern

Extension points, code paths, and implementation choices that should survive contact with production.

May 21, 2026 · 3 min read

WordPress Permalink Checklist After Migration: Catch URL Problems Early

A post-migration WordPress permalink checklist for checking rewrite rules, post URLs, archives, and redirect noise.

Technical media workspace representing image preparation and optimization before upload to WordPress.
03Build Pattern
Implementation Notes

Build Pattern

Extension points, code paths, and implementation choices that should survive contact with production.

May 21, 2026 · 3 min read

WordPress Image Optimization Checklist: What to Fix Before Upload

A practical WordPress image optimization checklist covering dimensions, compression, formats, and Media settings before upload.