* 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 string which contains the type attribute for script tag.
* If the current theme does not declare HTML5 support for 'script',
* then it initializes as `type='text/javascript'`.
public function __construct() {
add_action( 'init', array( $this, 'init' ), 0 );
function_exists( 'is_admin' ) && ! is_admin()
function_exists( 'current_theme_supports' ) && ! current_theme_supports( 'html5', 'script' )
$this->type_attr = " type='text/javascript'";
* Fires when the WP_Scripts instance is initialized.
* @param WP_Scripts $this 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 `$echo` parameter.
* @see print_extra_script()
* @param string $handle The script's registered handle.
* @param bool $echo Optional. Whether to echo the extra script
* instead of just returning it. Default true.
* @return bool|string|void Void if no data exists, extra scripts if `$echo` is true,
public function print_scripts_l10n( $handle, $echo = true ) {
_deprecated_function( __FUNCTION__, '3.3.0', 'WP_Scripts::print_extra_script()' );
return $this->print_extra_script( $handle, $echo );
* Prints extra scripts of a registered script.
* @param string $handle The script's registered handle.
* @param bool $echo Optional. Whether to echo the extra script
* instead of just returning it. Default true.
* @return bool|string|void Void if no data exists, extra scripts if `$echo` is true,
public function print_extra_script( $handle, $echo = true ) {
$output = $this->get_data( $handle, 'data' );
printf( "<script%s id='%s-js-extra'>\n", $this->type_attr, esc_attr( $handle ) );
// CDATA is not needed for HTML 5.
if ( $this->type_attr ) {
echo "/* <![CDATA[ */\n";
if ( $this->type_attr ) {
* 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 ( 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 ];
$conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : '';
$cond_before = "<!--[if {$conditional}]>\n";
$cond_after = "<![endif]-->\n";
$before_handle = $this->print_inline_script( $handle, 'before', false );
$after_handle = $this->print_inline_script( $handle, 'after', false );
$before_handle = sprintf( "<script%s id='%s-js-before'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $before_handle );
$after_handle = sprintf( "<script%s id='%s-js-after'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $after_handle );
if ( $before_handle || $after_handle ) {
$inline_script_tag = $cond_before . $before_handle . $after_handle . $cond_after;
$translations = $this->print_translations( $handle, false );
$translations = sprintf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $translations );
if ( $this->do_concat ) {
* Filters the script loader source.
* @param string $src Script loader source path.
* @param string $handle Script handle.
$srce = apply_filters( 'script_loader_src', $src, $handle );
if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle || $translations ) ) {
$this->do_concat = false;
// Have to print the so-far concatenated scripts right away to maintain the right order.
} elseif ( $this->in_default_dir( $srce ) && ! $conditional ) {
$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";
$has_conditional_data = $conditional && $this->get_data( $handle, 'data' );
if ( $has_conditional_data ) {
$this->print_extra_script( $handle );
if ( $has_conditional_data ) {
// 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 && 0 === strpos( $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( apply_filters( 'script_loader_src', $src, $handle ) );
$tag = $translations . $cond_before . $before_handle;
$tag .= sprintf( "<script%s src='%s' id='%s-js'></script>\n", $this->type_attr, $src, esc_attr( $handle ) );
$tag .= $after_handle . $cond_after;
* 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 );
return $this->add_data( $handle, $position, $script );
* Prints inline scripts registered for a specific handle.
* @param string $handle Name of the script to add the inline script to.
* @param string $position Optional. Whether to add the inline script
* before the handle or after. Default 'after'.
* @param bool $echo Optional. Whether to echo the script
* instead of just returning it. Default true.
* @return string|false Script on success, false otherwise.
public function print_inline_script( $handle, $position = 'after', $echo = true ) {
$output = $this->get_data( $handle, $position );
if ( empty( $output ) ) {
$output = trim( implode( "\n", $output ), "\n" );
printf( "<script%s id='%s-js-%s'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output );
* Localizes a script, only if the script has already been added.
* @param string $handle Name of the script to attach data to.
* @param string $object_name Name of the variable that will contain the data.
* @param array $l10n Array of data to localize.
* @return bool True on success, false on failure.
public function localize( $handle, $object_name, $l10n ) {
if ( 'jquery' === $handle ) {
if ( is_array( $l10n ) && isset( $l10n['l10n_print_after'] ) ) { // back compat, preserve the code in 'l10n_print_after' if present.
$after = $l10n['l10n_print_after'];
unset( $l10n['l10n_print_after'] );
if ( ! is_array( $l10n ) ) {
/* translators: 1: $l10n, 2: wp_add_inline_script() */
__( 'The %1$s parameter must be an array. To pass arbitrary data to scripts, use the %2$s function instead.' ),
'<code>wp_add_inline_script()</code>'
if ( is_string( $l10n ) ) {