WP Rocket preg_match Compilation Failed: Missing Closing Parenthesis in Delay JS
Stop WP Rocket flooding the PHP log with preg_match warnings by escaping brackets in the Delay JavaScript exclusions list.
The PHP error log fills with thousands of preg_match() warnings from WP Rocketβs Delay JavaScript Execution feature, in bursts each time a page is processed.
PHP Warning: preg_match(): Compilation failed: missing closing parenthesis at offset 4 in <wordpress-root>/wp-content/plugins/wp-rocket/inc/Engine/Optimization/DelayJS/HTML.php on line 221
PHP Warning: preg_match(): Compilation failed: missing closing parenthesis at offset 5 in <wordpress-root>/wp-content/plugins/wp-rocket/inc/Engine/Optimization/DelayJS/HTML.php on line 221
Cause
WP Rocket uses each Delay JS exclusion as a regular expression. It escapes +, ?ver and #, but not (, so an entry such as fbq( becomes the invalid pattern #fbq(#i.
The check runs for each script on the page until an exclusion matches, so most scripts hit every broken entry. The offset is the length of the entry: 4 for fbq(, 5 for gtag(.
The site keeps working, but the broken entries never match. Scripts they were meant to exclude are delayed until the first user interaction.
Fix
Open WP Rocket > Settings > File Optimization > Delay JavaScript Execution > Excluded JavaScript Files and escape every ( with a backslash. These entries were the ones affected here:
twq\(
gtag\(
fbq\(
ga\( '
ga\('
Save the settings, then clear the WP Rocket cache so pages are rebuilt with the corrected list.
To find invalid entries from the command line, run this from the WordPress root with WP-CLI:
wp eval '$list = get_option("wp_rocket_settings")["delay_js_exclusions"] ?? []; foreach ($list as $p) { if (@preg_match("#" . str_replace("#", "\#", $p) . "#i", "") === false) echo $p, PHP_EOL; }'
Verify
Run the command again and confirm it prints nothing. Then clear the PHP error log and browse the site. No new preg_match warnings should appear once pages have been rebuilt.
Notes
- The warnings only appear when WP Rocket processes a page, so cached and logged-in views do not add to them.
- Once the entries are fixed, those scripts run straight away instead of waiting for interaction. Analytics may record more accurately, at a small page speed cost.
/sdk.js#xfbmlis safe as it is, because WP Rocket escapes#itself.- Tested with WP Rocket 3.23 on PHP 8.5.