Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bewho/035e82439b33b5694edb363e8609f6a8 to your computer and use it in GitHub Desktop.
Save bewho/035e82439b33b5694edb363e8609f6a8 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