Title: Script order after merging scripts
Last modified: October 9, 2024

---

# Script order after merging scripts

 *  Resolved [blackfire0](https://wordpress.org/support/users/blackfire0/)
 * (@blackfire0)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/script-order-after-merging-scripts/)
 * My own scripts on my site are loaded after external scripts. I load all of them
   using the `wp_enqueue_script` function and specify the dependencies. However,
   when I enable script aggregation in Autoptimize, my own scripts are loaded before
   the external ones. How can I preserve the original order? Any solution would 
   be fine, including writing a custom plugin to address the issue.

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Plugin Author [Optimizing Matters](https://wordpress.org/support/users/optimizingmatters/)
 * (@optimizingmatters)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/script-order-after-merging-scripts/#post-18064097)
 * Aggregating is done long after assets are enqueued and only takes the exclusions(
   configured and some hardcoded) into account I’m afraid.
 * So looking at your HTML source, are your own scripts aggregated or do they remain
   separate?
 *  Thread Starter [blackfire0](https://wordpress.org/support/users/blackfire0/)
 * (@blackfire0)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/script-order-after-merging-scripts/#post-18064169)
 * Aggregated. This part is very well.
 * Before aggregate like:
   <link theme.css><link angular.min.css> (external)<link
   myscript1.css><link myscrip2.css>
 * After aggregate like:
   <link autoptimize_xy.css><link angular.min.css>
 *  Thread Starter [blackfire0](https://wordpress.org/support/users/blackfire0/)
 * (@blackfire0)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/script-order-after-merging-scripts/#post-18064358)
 * External: add_filter(‘wp_enqueue_scripts’, [$this, ‘loadScriptsFirst’], 0);
   My
   script: add_filter(‘wp_enqueue_scripts’, [$this, ‘loadScriptsLast’], 100);
 *  Plugin Author [Optimizing Matters](https://wordpress.org/support/users/optimizingmatters/)
 * (@optimizingmatters)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/script-order-after-merging-scripts/#post-18064501)
 * aha, an external file, AO indeed cannot aggregate those. what you could to is
   change the “injection point” for CSS with below code snippet;
 *     ```
       add_filter( 'autoptimize_filter_css_replacetag', 'blackfire_override_css_replacetag', 10, 1 );
       function blackfire_override_css_replacetag($replacetag) {
         return array( '</head>', 'before' );
       }
       ```
   
 * hope this helps,
    frank
 *  Thread Starter [blackfire0](https://wordpress.org/support/users/blackfire0/)
 * (@blackfire0)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/script-order-after-merging-scripts/#post-18065206)
 * Its worked, thanks!
 *  Plugin Author [Optimizing Matters](https://wordpress.org/support/users/optimizingmatters/)
 * (@optimizingmatters)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/script-order-after-merging-scripts/#post-18065870)
 * you’re welcome, feel free to [leave a review of the plugin and support here](https://wordpress.org/support/plugin/autoptimize/reviews/#new-post)!
   🙂

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Script order after merging scripts’ is closed to new replies.

 * ![](https://ps.w.org/autoptimize/assets/icon-256X256.png?rev=2211608)
 * [Autoptimize](https://wordpress.org/plugins/autoptimize/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/autoptimize/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/autoptimize/)
 * [Active Topics](https://wordpress.org/support/plugin/autoptimize/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/autoptimize/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/autoptimize/reviews/)

## Tags

 * [aggregation](https://wordpress.org/support/topic-tag/aggregation/)
 * [Merge](https://wordpress.org/support/topic-tag/merge/)

 * 6 replies
 * 2 participants
 * Last reply from: [Optimizing Matters](https://wordpress.org/support/users/optimizingmatters/)
 * Last activity: [1 year, 8 months ago](https://wordpress.org/support/topic/script-order-after-merging-scripts/#post-18065870)
 * Status: resolved