I have a few posts needing javascript, but I can't rely on plugins. I know it can be achieved through functions.php, but there's another problem: I don't want the code to be loaded on every page. I need each code to load on its specific post only. I've been searching for around 1 hour for a solution, but everything that I find is either suggesting a plugin or throw everything inside a function file and then the javascript code will load in every page. What I'm looking for is something like this:

If post is using [shortcode], then load js file postscript.js
  • You can easily write PHP that will conditionally enqueue scripts, yes. I assume you have an example that hooks wp_enqueue_scripts and then calls wp_enqueue_script: you should be able to add logic around the wp_enqueue_script calls fine. – Rup Jun 24 '20 at 8:07
  • Do you know where I can find a tutorial for that? – 023023 Jun 24 '20 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.