Skip to content

Instantly share code, notes, and snippets.

@glueckpress
Last active October 19, 2022 07:51
Show Gist options
  • Save glueckpress/7556296388346ff11aa9d536bc8acee5 to your computer and use it in GitHub Desktop.
Save glueckpress/7556296388346ff11aa9d536bc8acee5 to your computer and use it in GitHub Desktop.
[WordPress][WP Rocket] Barely tested hack to exclude a file from defer JS only, without having to exclude it from minification.
<?php
/**
* WP Rocket:
* 1. Customise order of minification and defer operations.
* 2. Exclude file from defer JS only (still minified).
*
* Tested rudimentary with WP Rocket 2.11.7 and 3.0.x.
* Test before using this in production!
*/
add_action( 'wp_rocket_loaded', function () {
// Remove default minify/defer priorities.
remove_filter( 'rocket_buffer', 'rocket_minify_process', 13 );
remove_filter( 'rocket_buffer', 'rocket_defer_js', 14 );
// Re-add minify/defer priorities (reversed).
add_filter( 'rocket_buffer', 'rocket_defer_js', 13 );
add_filter( 'rocket_buffer', 'rocket_minify_process', 14 );
// Exclude file from defer JS.
add_filter( 'rocket_exclude_defer_js', function ( $excluded_files = [] ) {
// Use original, not modified path to file!
$excluded_files[] = '/wp-content/original/path/to/file.js';
return $excluded_files;
} );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment