Update equeued file sources in WordPress

Today I have a problem when building obfuscation code for my team. After creating the uglify files, I need to replace all the original file sources with the uglify ones. The most important thing is I want the code still works with WP Rocket plugin.

After a quick research, I found that WordPress has many hooks to adjust the assets.

First of all, I think I need to list all the scripts and styles enqueued, so I found this link. It’s pretty cool that I can get all the things enqueued, but I can’t change the enqueued resources with this hook because it’s called when the resources already enqueued.

Because I just want to get all the enqueued scripts, so I tried with the filter script_loader_src which I’ve used to remove query string from some core WordPress scripts before. I think it’s the correct filter I need, it has the $source variable for me to update, and the $handle to filter out which scripts I need to replace. Cool! Let’s do the code!

The main purpose of this code is getting all the enqueued resources, filter out which has my theme prefix in the $handle to update the resource URL. The final code will be something like this:

That’s it 🎉