0

In a website I'm working on, a javascript file (and a css too) gets downloaded even if the feature it's related with is not present on the page. Since that plugin doesn't allow to load the files only when needed, I wanted to use a wordpress hook to dequeue them when not necessary.

To know if this scripts are needed, I could check inside the html code if a specific element is present (using regex or another method), but I don't know which hook to use and how to get the html data. Could anyone show me how to do it or even just point me to some documentation about it?

Drago96
  • 1,265
  • 10
  • 19
  • You don’t _have_ a DOM per se, on the WP side - so you would need to capture the complete page output first (or at least the relevant section), and then put it through a DOM parser … – CBroe May 25 '21 at 10:03
  • Yes, that's what I thought. If I could get the html code, I could also just run a Regex to find if the element is in there. Still, I don't know which hook to use to do this... Thank you however for your comment, I've updated my question to be more clear :) – Drago96 May 25 '21 at 10:22
  • You can check https://stackoverflow.com/questions/772510/wordpress-filter-to-modify-final-html-output for some info/inspiration. – CBroe May 25 '21 at 10:25
  • How does the feature get added? You can do some things like de-enqueing if a shortcode isn't present on a page (has_shortcode). Depends a bit on where it's added and how. Is anything unique in the_content? – Tom Woodward May 25 '21 at 10:43
  • Yes, the feature is actually added with a shortcode – Drago96 May 25 '21 at 15:43

0 Answers0