wp_default_packages_scripts( WP_Scripts $scripts )

Registers all the WordPress packages scripts that are in the standardized js/dist/ location.


Description

For the order of $scripts->add see wp_default_scripts.


Parameters

$scripts

(Required) WP_Scripts object.


Source

File: wp-includes/script-loader.php

function wp_default_packages_scripts( $scripts ) {
	$suffix  = wp_scripts_get_suffix();
	$version = classicpress_asset_version( 'script' );

	$assets = array(
		'a11y' => array(
			'dependencies' => array( 'wp-dom-ready', 'wp-i18n' ),
			'version'      => $version,
		),
		'api-fetch' => array(
			'dependencies' => array( 'wp-i18n', 'wp-url' ),
			'version'      => $version,
		),
		'dom-ready' => array(
			'version'      => $version,
		),
		'hooks' => array(
			'version' => $version,
		),
		'i18n' => array(
			'dependencies' => array( 'wp-hooks' ),
			'version'      => $version,
		),
		'url' => array(
			'version' => $version,
		),
	);

	foreach ( $assets as $file_name => $package_data ) {
		$handle   = 'wp-' . $file_name;
		$path     = "/wp-includes/js/dist/{$file_name}{$suffix}.js";

		if ( ! empty( $package_data['dependencies'] ) ) {
			$dependencies = $package_data['dependencies'];
		} else {
			$dependencies = array();
		}

		// Add dependencies that cannot be detected and generated by build tools.
		switch ( $handle ) {
			case 'wp-edit-post':
				array_push( $dependencies, 'media-models', 'media-views', 'postbox', 'wp-dom-ready' );
				break;
			case 'wp-preferences':
				array_push( $dependencies, 'wp-preferences-persistence' );
				break;
		}

		$scripts->add( $handle, $path, $dependencies, $package_data['version'], 1 );

		if ( in_array( 'wp-i18n', $dependencies, true ) ) {
			$scripts->set_translations( $handle );
		}

		/*
		 * Manually set the text direction localization after wp-i18n is printed.
		 * This ensures that wp.i18n.isRTL() returns true in RTL languages.
		 * We cannot use $scripts->set_translations( 'wp-i18n' ) to do this
		 * because WordPress prints a script's translations *before* the script,
		 * which means, in the case of wp-i18n, that wp.i18n.setLocaleData()
		 * is called before wp.i18n is defined.
		 */
		if ( 'wp-i18n' === $handle ) {
			$ltr    = _x( 'ltr', 'text direction' );
			$script = sprintf( "wp.i18n.setLocaleData( { 'text direction\u0004ltr': [ '%s' ] } );", $ltr );
			$scripts->add_inline_script( $handle, $script, 'after' );
		}
	}
}


Changelog

Changelog
Version Description
5.0.0 Introduced.