0

I noticed that almost all the plugins I installed load the css and javascript in the head of all pages, even those where the plugins are not used.
I think this is slowing down the loading of pages on my site a lot.
Is there a way to disable this thing? if so, how do you have a plugin to recommend or should I use the code?

3
  • There's an old question about implementing that, but I think the general problem is it's difficult to know when you're setting up the page header what's going to be on the page. What sort of plugins are they? Would you be able to work out in advance when they'll be needed?
    – Rup
    Commented Jun 13, 2023 at 13:25
  • The solution to this would differ for each plugin. You need to identify the problematic plugins and then ask their authors individually. Commented Jun 13, 2023 at 14:08
  • hi @Rup , thanks for the replies, the plug-ins are different for example: contact form 7, contact form 7 styler forn Divi, gutena tabs, attachment for WP, the events calenar... plus the RSS feeds I don't want and others. I've seen that there are other plug-ins like the caching ones that do something similar to what I need but I've never tried them, do you have one to recommend? i found this too [wordpress.com/plugins/wp-asset-clean-up]
    – LesCa
    Commented Jun 13, 2023 at 20:31

1 Answer 1

0

Most plugins will load their assets in the header on all pages, because they know very little about your site, and how you're using the plugin. This is a reason why it's generally advisable to use plugins only if you have to: they're built to work for as many people as possible, which means they're not as optimized for your site as they could be.

Loading plugin scripts and styles conditionally (ex: specific page) could end up being challenging, especially as the site grows, as you'll be maintaining conditions for the entire site, and plugins change.

Your first step will be to use the wp_dequeue_script() and wp_dequeue_style() functions to prevent the assets from loading, and then re-enqueue them behind your conditional logic. Make sure you call those functions after the plugin has enqueued its assets, otherwise, the assets won't be dequeued. Typically the wp_enqueue_scripts hook with a late priority should work (untested):

add_action( 'wp_enqueue_scripts', static function () {
    wp_dequeue_script( ... );
}, 999 );
4
  • hello @Caleb , for example I don't need contact form 7 in all pages if the form is only in the contact page and the same for the events ...
    – LesCa
    Commented Jun 13, 2023 at 20:38
  • Yep, that makes sense. You'll have to override CF7's default enqueueing function, and then manually enqueue it's assets on specific pages.
    – Caleb
    Commented Jun 13, 2023 at 20:42
  • Do you recommend loading it later instead of not loading it at all?
    – LesCa
    Commented Jun 13, 2023 at 20:55
  • CF7 probably won't work/display correctly without it's assets.
    – Caleb
    Commented Jun 13, 2023 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.