WordPress.org

Make WordPress Core

Opened 10 months ago

Last modified 6 months ago

#25277 new feature request

WP_scripts does not allow to add data after the enque has been added to HTML

Reported by: hakre Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.6
Component: Script Loader Keywords: has-patch
Focuses: Cc:

Description

For example if a template part makes use of a jQuery plugin that needs jQuery but additionally some javascript code to initialize the plugin which is not inside a js file, the data "data" can not be used to set that initialization code for the enqueued script becuse it would be put before the resource is loaded:

Example Code:

$wp_scripts->add("jquery.webticker", get_stylesheet_directory_uri() . "/js/jquery.webticker.js", array("jquery"));
$wp_scripts->add_data("jquery.webticker", "data", "jQuery('#webticker').webTicker();");
$wp_scripts->enqueue("jquery.webticker");

Example Output:

<script type='text/javascript'>
/* <![CDATA[ */
jQuery('#webticker').webTicker();
/* ]]> */
</script>
<script type='text/javascript' src='http://example.com/wp-content/themes/example/js/jquery.webticker.js?ver=3.6'></script>

This feature request suggests to allow an additional key to be used instead of "data" (here exemplary "data-after") to place script contents *after* the enqueued script:

Example Code (after patch):

$wp_scripts->add("jquery.webticker", get_stylesheet_directory_uri() . "/js/jquery.webticker.js", array("jquery"));
$wp_scripts->add_data("jquery.webticker", "data-after", "jQuery('#webticker').webTicker();");
                                          ############
$wp_scripts->enqueue("jquery.webticker");

Example Output (after patch):

<script type='text/javascript' src='http://example.com/wp-content/themes/example/js/jquery.webticker.js?ver=3.6'></script>
<script type='text/javascript'>
/* <![CDATA[ */
jQuery('#webticker').webTicker();
/* ]]> */
</script>

Attachments (1)

wp-25277-data-after.patch (7.0 KB) - added by hakre 10 months ago.

Download all attachments as: .zip

Change History (2)

comment:1 nacin6 months ago

  • Component changed from General to Script Loader
Note: See TracTickets for help on using tickets.