Blog / DevOps / Speedup WordPress By Dequeue Unused Scripts

Speedup WordPress By Dequeue Unused Scripts

Page Speed

Core Web Vitals is coming. Users prefer faster pages, so on search, faster pages (that are relevant) will be returned first. Recently I installed “The Events Calendar” for WordPress. I found it was loading a large amount of Javascript and CSS on ever page. But I only use it on /events (and its own post types). What do to to speedup wordpress?

In my Quest for Web Site Performance Perfection, I found these enqueues scripts, no matter whether you minifed or preloaded, were causing a lot of excess CPU and parse time. An infinitely fast network cannot help. Its probably also using your battery up. So, let’s dig in.

First, we have to track them all down. To do this I added an echo statement to where they were enqueued. There may be a more elegant way by reading the fine documentation, but, I was in a hurry and this worked.

Once done, I added this below function to my theme functions. What it does is “if this page is a tribe_event or /events then allow else dequeue”. This will speedup wordpress: less things sent to user.

Install, load, done. Now I have 42 less scripts/js enqueued, and this means more CPU for parsing other things, and, more battery for you the reader.

You can test your changes with the Chrome Lighthouse, or web.dev. Efficiency is in everyones interest, so save a bit, save a watt, speedup wordpress.

add_action( 'wp_enqueue_scripts', function() {
global $wp;
$post_type = get_post_type();
if ($post_type != "tribe_events" && $wp->request != "/events/") {
wp_dequeue_style('tribe-tooltip-css');
wp_dequeue_script('tribe-tooltip-js');
$junk_script = [
"event-tickets-attendees-list-js",
"event-tickets-details-js",
"event-tickets-tickets-rsvp-js"
];
$junk_style = [
"event-tickets-reset-css",
"event-tickets-tickets-css",
"event-tickets-tickets-rsvp-css",
"promoter",
"tribe-events-admin-menu",
"tribe-events-bar",
"tribe-events-block-classic-event-details",
"tribe-events-block-event-category",
"tribe-events-block-event-datetime",
"tribe-events-block-event-links",
"tribe-events-block-event-organizer",
"tribe-events-block-event-price",
"tribe-events-block-event-tags",
"tribe-events-block-event-venue",
"tribe-events-block-event-website",
"tribe-events-block-featured-image",
"tribe-events-calendar-full-mobile-style",
"tribe-events-calendar-mobile-style",
"tribe-events-calendar-script",
"tribe-events-calendar-style",
"tribe-events-dynamic",
"tribe-events-full-calendar-style",
"tribe-events-views-v2-accordion",
"tribe-events-views-v2-bootstrap-datepicker",
"tribe-events-views-v2-bootstrap-datepicker-styles",
"tribe-events-views-v2-breakpoints",
"tribe-events-views-v2-datepicker",
"tribe-events-views-v2-events-bar",
"tribe-events-views-v2-events-bar-inputs",
"tribe-events-views-v2-full",
"tribe-events-views-v2-month-grid",
"tribe-events-views-v2-month-mobile-events",
"tribe-events-views-v2-multiday-events",
"tribe-events-views-v2-navigation-scroll",
"tribe-events-views-v2-skeleton",
"tribe-events-views-v2-tooltip",
"tribe-events-views-v2-viewport",
"tribe-events-views-v2-view-selector",
"tribe-tooltip"
];
wp_deregister_script($junk_script);
wp_deregister_style($junk_style);
}
}, 99);

Leave a Reply

Your email address will not be published.