I have a function inside a class which runs a javascript code. It's the following:

public static function notReady() {
    if (!document.hidden) {
       // Some operations...

The question is: how can I run this code without using wp_head hook. When I use wp_head the code comes to be run throughout all the pages of the website, which is not what I aim to do. I want this code to be run whenever the plugin which is using it, is being run in the first place. So, what is the way to run this code without using wp_head or wp_footer?

  • 1
    You can still hook into wp_head but only do things on certain pages. Running the JS without hooking into wp_head is not your problem, the JS snippet being on every page is your problem, and that's what you should have asked about. It's also an easier simpler question to answer than what you asked. – Tom J Nowell Jan 11 at 14:16

Just make own notReady.php file for your function and add where you want to use with include("notReady.php") or require("notReady.php")... or make your own javascript file and load it within html:

<script src="notReady.js" />

Basicly, try to load your scripts at bottom your website... This is just optimalisation

Your Answer

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

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