Posted on September 25, 2019 in WordPress
One of my biggest pet peeves with WordPress plugins is when authors enqueue scripts and styles on pages other than what they are needed on. For example, I’m using The Events Calendar (which seems to be a great plugin – perfect for my client’s use case) and I am only using it on two pages – the main events page and single event pages. But no matter what page I’m on, the plugin loads 4 extra JS files and 4 extra CSS files (including jQuery and jQuery UI :shakesfist:). It’s utterly pointless and an extra burden our users have to endure to download those extra files for nothing when they hit the home page or any other page other than the events ones.
Disclaimer: I’m kind of a nut when it comes to squeezing out every piece of performance I can from a site. As you might be able to tell.
So I did some digging in the plugin’s source code to find where assets are enqueued and if I could figure out the names of them to dequeue them in my theme. Good news – I found them and have the solution!
It turns out that there was just one offender that TEC enqueues on every page, tooltips. And for whatever reason it loads in a total of 8 assets.
In my theme’s functions.php, I simply dequeued the styles and scripts for the tooltips like this:
And boom, those extra assets are no longer loaded. And as it turns out, they apparently aren’t even needed on the events pages from what I can tell. Tooltips seem to work just fine. So that’s a bit confusing, but if I do find out they’re needed, I’ll just wrap the dequeue hooks in a conditional that only fires if it’s not the event pages.
How I found the correct asset names to dequeue
To know for sure what assets were being enqueued on pages other than the event pages, I went into
common/src/Tribe/Assets.php in the plugin’s code and threw an
echo $asset->slug; on lines 229 and 261. That outputted the slugs at the very top of the document. If you do that and encounter additional names, you can just dequeue those as well.
Hope this helps someone! I know I will be back to reference this article the next time I use The Events Calendar and get pissed at the unnecessary assets it loads.