Loading plugins only on specific pages


Some plugins I use (I think about Events and Lightslider especially) are not needed in every page, but their css and js are always loaded. It makes some pages unnecessarily heavy and I would like to avoid that by loading these plugins only on some pages with specific templates.
I imagine it’s not so difficult to achieve in php, but I’m not familiar with this language and I haven’t tried to dig into php files so far… could someone help me with that?


1 Like

@jordan ,
yes, your idea of loading assets only on the pages where needed via template is right.
all you have to do is writing the correct template.
as an example, you can take this template as a reference.
no PHP expertise needed here :smiley:

1 Like

@hoernerfranz, I’m afraid your suggestion will not work…

Both ‘LightSlider’ and ‘Event’ plugin add css and js assets in PHP and unfortunately, there is no check in PHP if assets should be loaded or not.

I believe you’ll have to open issue for each plugin. Just checked Events and it loads assets on every page

Didn’t even check the other. Every plugin might be different in how they load assets.

ah, yes, of course the mechanism of loading assets is unique to each plugin, and, @pamtbaau you are right in that my suggestion would not work with the mentioned plugins without changes to the plugin’s php source.
my intention was rather to provide an example how to do this.
so the OP could
create an issue as @Karmalakas proposed
adapt the beforementioned plugins and create a pull request

Thanks for your answers.
I will create an issue for the plugins concerned. And in the meantime I’ll have to modify the plugin’s php file by adding a condition to the loading of the assets.
That’s what I’ve done for the plugin event and it does the job. In the public function onTwigSiteVariables() of the file events.php, it looks like this

if ($page->template() == 'calendar') {
  $js = 'plugin://events/assets/events.min.js';
  $assets->addJs($js, 1);

  // styles
  $css = 'plugin://events/assets/events.min.css';
1 Like