diff --git a/wp-includes/class.wp-scripts.php b/wp-includes/class.wp-scripts.php
index 5d8fb17ae3..d63c6bd973 100644
--- a/wp-includes/class.wp-scripts.php
+++ b/wp-includes/class.wp-scripts.php
@@ -25,6 +25,7 @@ class WP_Scripts extends WP_Dependencies {
public $concat_version = '';
public $do_concat = false;
public $print_html = '';
+ public $print_html_before = '';
public $print_code = '';
public $ext_handles = '';
public $ext_version = '';
@@ -144,6 +145,17 @@ class WP_Scripts extends WP_Dependencies {
$cond_after = "\n";
}
+ $before_handle = $this->print_inline_script( $handle, 'before', false );
+ $after_handle = $this->print_inline_script( $handle, 'after', false );
+
+ if ( $before_handle ) {
+ $before_handle = sprintf( "\n", $before_handle );
+ }
+
+ if ( $after_handle ) {
+ $after_handle = sprintf( "\n", $after_handle );
+ }
+
if ( $this->do_concat ) {
/**
* Filter the script loader source.
@@ -154,7 +166,12 @@ class WP_Scripts extends WP_Dependencies {
* @param string $handle Script handle.
*/
$srce = apply_filters( 'script_loader_src', $src, $handle );
- if ( $this->in_default_dir( $srce ) && ! $conditional ) {
+
+ if ( $before_handle && ! $conditional ) {
+ $this->print_html_before .= $before_handle;
+ }
+
+ if ( $this->in_default_dir( $srce ) && ! $conditional && ! $after_handle ) {
$this->print_code .= $this->print_extra_script( $handle, false );
$this->concat .= "$handle,";
$this->concat_version .= "$handle$ver";
@@ -195,7 +212,7 @@ class WP_Scripts extends WP_Dependencies {
if ( ! $src )
return true;
- $tag = "{$cond_before}\n{$cond_after}";
+ $tag = "{$cond_before}{$before_handle}\n{$after_handle}{$cond_after}";
/**
* Filter the HTML script tag of an enqueued script.
@@ -209,7 +226,11 @@ class WP_Scripts extends WP_Dependencies {
$tag = apply_filters( 'script_loader_tag', $tag, $handle, $src );
if ( $this->do_concat ) {
- $this->print_html .= $tag;
+ if ( $after_handle ) {
+ $this->print_html_before .= $tag;
+ } else {
+ $this->print_html .= $tag;
+ }
} else {
echo $tag;
}
@@ -217,6 +238,61 @@ class WP_Scripts extends WP_Dependencies {
return true;
}
+ /**
+ * Add extra code to a registered script.
+ *
+ * @since 4.5.0
+ *
+ * @param string $handle Name of the script to add the inline script to. Must be lowercase.
+ * @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 ( ! $data ) {
+ return false;
+ }
+
+ if ( 'after' !== $position ) {
+ $position = 'before';
+ }
+
+ $script = (array) $this->get_data( $handle, $position );
+ $script[] = $data;
+
+ return $this->add_data( $handle, $position, $script );
+ }
+
+ /**
+ * Print inline scripts registered for a specific handle.
+ *
+ * @since 4.5.0
+ *
+ * @param string $handle Name of the script to add the inline script to. Must be lowercase.
+ * @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 ) ) {
+ return false;
+ }
+
+ $output = trim( implode( "\n", $output ), "\n" );
+
+ if ( $echo ) {
+ printf( "\n", $output );
+ }
+
+ return $output;
+ }
+
/**
* Localizes a script, only if the script has already been added
*
@@ -339,6 +415,7 @@ class WP_Scripts extends WP_Dependencies {
$this->concat = '';
$this->concat_version = '';
$this->print_html = '';
+ $this->print_html_before = '';
$this->ext_version = '';
$this->ext_handles = '';
}
diff --git a/wp-includes/functions.wp-scripts.php b/wp-includes/functions.wp-scripts.php
index c83a9d1d93..68424e8ed8 100644
--- a/wp-includes/functions.wp-scripts.php
+++ b/wp-includes/functions.wp-scripts.php
@@ -85,6 +85,35 @@ function wp_print_scripts( $handles = false ) {
return wp_scripts()->do_items( $handles );
}
+/**
+ * Add extra code to a registered script.
+ *
+ * Code will only be added if the script in already in the queue.
+ * Accepts a string $data containing the Code. If two or more code blocks
+ * are added to the same script $handle, they will be printed in the order
+ * they were added, i.e. the latter added code can redeclare the previous.
+ *
+ * @since 4.5.0
+ *
+ * @see WP_Scripts::add_inline_script()
+ *
+ * @param string $handle Name of the script to add the inline script to. Must be lowercase.
+ * @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.
+ */
+function wp_add_inline_script( $handle, $data, $position = 'after' ) {
+ _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
+
+ if ( false !== stripos( $data, '' ) ) {
+ _doing_it_wrong( __FUNCTION__, __( 'Do not pass script tags to wp_add_inline_script().' ), '4.5.0' );
+ $data = trim( preg_replace( '##is', '$1', $data ) );
+ }
+
+ return wp_scripts()->add_inline_script( $handle, $data, $position );
+}
+
/**
* Register a new script.
*
diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php
index 826e2ac1fe..86c0661b5c 100644
--- a/wp-includes/script-loader.php
+++ b/wp-includes/script-loader.php
@@ -992,6 +992,10 @@ function _print_scripts() {
echo "\n";
}
+ if ( ! empty( $wp_scripts->print_html_before ) ) {
+ echo $wp_scripts->print_html_before;
+ }
+
$concat = str_split( $concat, 128 );
$concat = 'load%5B%5D=' . implode( '&load%5B%5D=', $concat );
diff --git a/wp-includes/version.php b/wp-includes/version.php
index be2af7e4b6..04275afd13 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
-$wp_version = '4.5-alpha-36632';
+$wp_version = '4.5-alpha-36633';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.