Categories
WordPress

Quick Tip: Script Debugging in WordPress

If you’re debugging core WordPress scripts, one thing you might run into is dealing with cached copies of the script. Due to how script-loader.php enqueues the core files, their versions are “hard coded” and short of editing script-loader.php as well, there’s a way to fix this via a filter:

add_filter( 'script_loader_src', function( $src, $handle ) {
     if ( false !== strpos( $src, 'ver=' ) ) {
         $src = remove_query_arg( 'ver', $src );
         $src = add_query_arg( array( 'ver', rawurlencode( uniqid( $handle ) . '-' ) ), $src );
     }
     
     return $src;
 }, -1, 2 );

This will apply a unique ver argument to the core scripts on each refresh, so no matter what you’re editing you should get the most recent version from both any page cache you may have and also the browser cache (🤞).

Also, don’t forget to enable SCRIPT_DEBUG if you’re hacking away at core scripts to debug issues.

I couldn’t find a good related image, so enjoy this delicious toilet paper.

Leave a Reply