0

In WordPress a plugin may add extra data to be printed before or after a script when enqueueing a script. Quite a few plugins that we have enabled do this to define for example an ajax URL etc and it basically looks like this:

<script type='text/javascript' src=...'></script>
<script type='text/javascript'>
/* <![CDATA[ */
var xyzParams = {...};
/* ]]> */
</script>
<script type='text/javascript' src=...'></script>
<script type='text/javascript'>
/* <![CDATA[ */
var xyzParams = {...};
/* ]]> */
</script>
<script type='text/javascript' src=...'></script>

The problem now is that the performance optimisation plugin we use (Total Cache) cannot minimize and concatenate these 3 scripts into one because there are other scripts in between resulting in 3 requests instead of one. I would ideally want to print all scripts that don't include a src to be printed first before all others. On our website this would work just fine and improve performance.

Those scripts are printed by $this->print_extra_script( $handle ); in wp-scripts.php and the plugin registers these by using eg wp_localize_script;

So far I couldn't figure out how to change the order. My only thought so far is to use PHP and Regex before the HTML is being echoed. I couldn't find other plugins that do this yet. Any other thoughts besides using Regex to do it more in a WordPress way?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.