* Dependencies API: WP_Scripts class
* @subpackage Dependencies
* Core class used to register scripts.
class WP_Scripts extends WP_Dependencies {
* Full URL with trailing slash.
* URL of the content directory.
* Default version string for scripts.
* Holds handles of scripts which are enqueued in footer.
public $in_footer = array();
* Holds a list of script handles which will be concatenated.
* Holds a string which contains script handles and their version.
public $concat_version = '';
* Whether to perform concatenation.
public $do_concat = false;
* Holds HTML markup of scripts and additional data if concatenation
* Holds inline code if concatenation is enabled.
* Holds a list of script handles which are not in the default directory
* if concatenation is enabled.
public $ext_handles = '';
* Holds a string which contains handles and versions of scripts which
* are not in the default directory if concatenation is enabled.
public $ext_version = '';
* List of default directories.
* Holds a mapping of dependents (as handles) for a given script handle.
* Used to optimize recursive dependency tree checks.
* @var array<string, string[]>
private $dependents_map = array();
* Holds a reference to the delayed (non-blocking) script loading strategies.
* Used by methods that validate loading strategies.
private $delayed_strategies = array( 'defer', 'async' );
public function __construct() {
add_action( 'init', array( $this, 'init' ), 0 );
* Fires when the WP_Scripts instance is initialized.
* @param WP_Scripts $wp_scripts WP_Scripts instance (passed by reference).
do_action_ref_array( 'wp_default_scripts', array( &$this ) );
* Prints the scripts passed to it or the print queue. Also prints all necessary dependencies.
* @since 2.8.0 Added the `$group` parameter.
* @param string|string[]|false $handles Optional. Scripts to be printed: queue (false),
* single script (string), or multiple scripts (array of strings).
* @param int|false $group Optional. Group level: level (int), no groups (false).
* @return string[] Handles of scripts that have been printed.
public function print_scripts( $handles = false, $group = false ) {
return $this->do_items( $handles, $group );
* Prints extra scripts of a registered script.
* @since 2.8.0 Added the `$display` parameter.
* @see print_extra_script()
* @param string $handle The script's registered handle.
* @param bool $display Optional. Whether to print the extra script
* instead of just returning it. Default true.
* @return bool|string|void Void if no data exists, extra scripts if `$display` is true,
public function print_scripts_l10n( $handle, $display = true ) {
_deprecated_function( __FUNCTION__, '3.3.0', 'WP_Scripts::print_extra_script()' );
return $this->print_extra_script( $handle, $display );
* Prints extra scripts of a registered script.
* @param string $handle The script's registered handle.
* @param bool $display Optional. Whether to print the extra script
* instead of just returning it. Default true.
* @return bool|string|void Void if no data exists, extra scripts if `$display` is true,
public function print_extra_script( $handle, $display = true ) {
$output = $this->get_data( $handle, 'data' );
* Do not print a sourceURL comment if concatenation is enabled.
* Extra scripts may be concatenated into a single script.
* The line-based sourceURL comments may break concatenated scripts
* and do not make sense when multiple scripts are joined together.
if ( ! $this->do_concat ) {
rawurlencode( "{$handle}-js-extra" )
wp_print_inline_script_tag( $output, array( 'id' => "{$handle}-js-extra" ) );
* Checks whether all dependents of a given handle are in the footer.
* If there are no dependents, this is considered the same as if all dependents were in the footer.
* @param string $handle Script handle.
* @return bool Whether all dependents are in the footer.
private function are_all_dependents_in_footer( $handle ) {
foreach ( $this->get_dependents( $handle ) as $dep ) {
if ( isset( $this->groups[ $dep ] ) && 0 === $this->groups[ $dep ] ) {
* Processes a script dependency.
* @since 2.8.0 Added the `$group` parameter.
* @see WP_Dependencies::do_item()
* @param string $handle The script's registered handle.
* @param int|false $group Optional. Group level: level (int), no groups (false).
* @return bool True on success, false on failure.
public function do_item( $handle, $group = false ) {
if ( ! parent::do_item( $handle ) ) {
if ( 0 === $group && $this->groups[ $handle ] > 0 ) {
$this->in_footer[] = $handle;
if ( false === $group && in_array( $handle, $this->in_footer, true ) ) {
$this->in_footer = array_diff( $this->in_footer, (array) $handle );
$obj = $this->registered[ $handle ];
if ( $obj->extra['conditional'] ?? false ) {
if ( null === $obj->ver ) {
$ver = $obj->ver ? $obj->ver : $this->default_version;
if ( isset( $this->args[ $handle ] ) ) {
$ver = $ver ? $ver . '&' . $this->args[ $handle ] : $this->args[ $handle ];
$strategy = $this->get_eligible_loading_strategy( $handle );
$intended_strategy = (string) $this->get_data( $handle, 'strategy' );
if ( ! $this->is_delayed_strategy( $intended_strategy ) ) {
* Move this script to the footer if:
* 1. The script is in the header group.
* 2. The current output is the header.
* 3. The intended strategy is delayed.
* 4. The actual strategy is not delayed.
* 5. All dependent scripts are in the footer.
0 === $this->groups[ $handle ] &&
! $this->is_delayed_strategy( $strategy ) &&
$this->are_all_dependents_in_footer( $handle )
$this->in_footer[] = $handle;
$before_script = $this->get_inline_script_tag( $handle, 'before' );
$after_script = $this->get_inline_script_tag( $handle, 'after' );
if ( $before_script || $after_script ) {
$inline_script_tag = $before_script . $after_script;
* Prevent concatenation of scripts if the text domain is defined
* to ensure the dependency order is respected.
$translations_stop_concat = ! empty( $obj->textdomain );
$translations = $this->print_translations( $handle, false );
* The sourceURL comment is not included by WP_Scripts::print_translations()
* when `$display` is `false` to prevent issues where the script tag contents are used
* by extenders for other purposes, for example concatenated with other script content.
* Include the sourceURL comment here as it would be when printed directly.
$source_url = rawurlencode( "{$handle}-js-translations" );
$translations .= "\n//# sourceURL={$source_url}";
$translations = wp_get_inline_script_tag( $translations, array( 'id' => "{$handle}-js-translations" ) );
if ( $this->do_concat ) {
* Filters the script loader source.
* @param string $src Script loader source path.
* @param string $handle Script handle.
$filtered_src = apply_filters( 'script_loader_src', $src, $handle );
$this->in_default_dir( $filtered_src )
&& ( $before_script || $after_script || $translations_stop_concat || $this->is_delayed_strategy( $strategy ) )
$this->do_concat = false;
// Have to print the so-far concatenated scripts right away to maintain the right order.
} elseif ( $this->in_default_dir( $filtered_src ) ) {
$this->print_code .= $this->print_extra_script( $handle, false );
$this->concat .= "$handle,";
$this->concat_version .= "$handle$ver";
$this->ext_handles .= "$handle,";
$this->ext_version .= "$handle$ver";
$this->print_extra_script( $handle );
// A single item may alias a set of items, by having dependencies, but no source.
if ( $inline_script_tag ) {
if ( $this->do_concat ) {
$this->print_html .= $inline_script_tag;
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && str_starts_with( $src, $this->content_url ) ) ) {
$src = $this->base_url . $src;
$src = add_query_arg( 'ver', $ver, $src );
/** This filter is documented in wp-includes/class-wp-scripts.php */
$src = esc_url_raw( apply_filters( 'script_loader_src', $src, $handle ) );
$attr[ $strategy ] = true;
if ( $intended_strategy ) {
$attr['data-wp-strategy'] = $intended_strategy;
// Determine fetchpriority.
$original_fetchpriority = isset( $obj->extra['fetchpriority'] ) ? $obj->extra['fetchpriority'] : null;
if ( null === $original_fetchpriority || ! $this->is_valid_fetchpriority( $original_fetchpriority ) ) {
$original_fetchpriority = 'auto';
$actual_fetchpriority = $this->get_highest_fetchpriority_with_dependents( $handle );
if ( null === $actual_fetchpriority ) {
// If null, it's likely this script was not explicitly enqueued, so in this case use the original priority.
$actual_fetchpriority = $original_fetchpriority;
if ( is_string( $actual_fetchpriority ) && 'auto' !== $actual_fetchpriority ) {
$attr['fetchpriority'] = $actual_fetchpriority;
if ( $original_fetchpriority !== $actual_fetchpriority ) {
$attr['data-wp-fetchpriority'] = $original_fetchpriority;
$tag = $translations . $before_script;
$tag .= wp_get_script_tag( $attr );
* Filters the HTML script tag of an enqueued script.
* @param string $tag The `<script>` tag for the enqueued script.
* @param string $handle The script's registered handle.
* @param string $src The script's source URL.
$tag = apply_filters( 'script_loader_tag', $tag, $handle, $src );
if ( $this->do_concat ) {
$this->print_html .= $tag;
* Adds extra code to a registered script.
* @param string $handle Name of the script to add the inline script to.
* @param string $data String containing the JavaScript to be added.
* @param string $position Optional. Whether to add the inline script
* before the handle or after. Default 'after'.
* @return bool True on success, false on failure.
public function add_inline_script( $handle, $data, $position = 'after' ) {
if ( 'after' !== $position ) {
$script = (array) $this->get_data( $handle, $position );