Title: Filter hook for modifying inline javascript
Last modified: November 5, 2021

---

# Filter hook for modifying inline javascript

 *  Resolved [Yan](https://wordpress.org/support/users/yankiara/)
 * (@yankiara)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/filter-hook-for-modifying-inline-javascript/)
 * Hi!
    First, congratulations for this great plugin!!! So far, this is the best
   optimization plugin I could test, even in its free version! Code is very well
   optimized but doesn’t break sites (I mean not too extreme like others…).
 * I have a problem with some inline javascript, though, which prevents me from 
   deferring jquery.js (or other dependancies).
 * So I would like to replace some inline JS like this:
    document.addEventListener(“
   DOMContentLoaded”, function() { INLINE JS CODE });
 * This way, it will be executed only when DOM is ready, and I can deferr JS dependancies.
   
   I know how to write PHP for this, but now I need to execute it at the right moment.
 * Is there a filter hook I could use to filter html content output?
    I couldn’t
   find any documentation on this.
 * Thanks in advance.

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

 *  [Harshad](https://wordpress.org/support/users/bornforphp/)
 * (@bornforphp)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/filter-hook-for-modifying-inline-javascript/#post-15044180)
 * [@yankiara](https://wordpress.org/support/users/yankiara/) I’ll have to check
   this with our development team if there’s any hook/filter and get back to you
   is there’s any.
 * Thank you for your time and patience.
 *  [Harshad](https://wordpress.org/support/users/bornforphp/)
 * (@bornforphp)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/filter-hook-for-modifying-inline-javascript/#post-15054175)
 * [@yankiara](https://wordpress.org/support/users/yankiara/) Are you using inline
   javascript in HTML? or `wp_add_inline_script`
 * If you are using inline Javascript in HTML then there is no filter, else if you
   are using `wp_add_inline_script` our development team can share a snippet with
   you that can work.
 *  Thread Starter [Yan](https://wordpress.org/support/users/yankiara/)
 * (@yankiara)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/filter-hook-for-modifying-inline-javascript/#post-15057814)
 * Hi,
    And thanks for your reply!
 * It is actually inline JS from theme/plugins, so I have no control over it, that’s
   the problem 🙁
 * But for those who are interested, in the meantime I wrote a little script that
   solves the problem in a rather brute way, filtering the whole html ouput to delay
   JS execution with DOMContentLoaded:
 *     ```
       <?php
   
       function callback($buffer) {
           preg_match_all( "/<script(?:[^>]*)>(?<content>[\s\S]*?)<\/script>/ims", $buffer, $matches, PREG_SET_ORDER );
       	foreach ( $matches as $code ) {
       		if ( strpos( $code['content'], 'jQuery(' ) === false ) {
       			continue;
       		}
       		if ( strpos( $code['content'], 'DOMContentLoaded' ) !== false ) {
       			continue;
       		}
       		if ( empty( $code['content'] ) ) {
       			continue;
       		}
       		if ( preg_match( '/(application\/ld\+json)/i', $code[0] ) ) {
       			continue;
       		}
   
       		$new_code = "<script>document.addEventListener('DOMContentLoaded', () => { " . $code['content'] . "});</script>";
       		$buffer = str_replace( $code[0], $new_code, $buffer );
       	}
           return $buffer;
       }
   
       function buffer_start() { ob_start("callback"); }
   
       function buffer_end() { ob_end_flush(); }
   
       add_action('wp_loaded', 'buffer_start');
       add_action('shutdown', 'buffer_end');
       ```
   
 * (inspired from [https://katsarov.info/3-code-snippets-core-web-vitals/](https://katsarov.info/3-code-snippets-core-web-vitals/))
 * It might not be the most optimized way, but couldn’t find a better one, since
   some plugins add inline JS in different places.
 * Thanks again for your work and support.

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

The topic ‘Filter hook for modifying inline javascript’ is closed to new replies.

 * ![](https://ps.w.org/wp-optimize/assets/icon-256x256.png?rev=1552899)
 * [WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance](https://wordpress.org/plugins/wp-optimize/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-optimize/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-optimize/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-optimize/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-optimize/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-optimize/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [Yan](https://wordpress.org/support/users/yankiara/)
 * Last activity: [4 years, 9 months ago](https://wordpress.org/support/topic/filter-hook-for-modifying-inline-javascript/#post-15057814)
 * Status: resolved