I have a function inside a class which runs a javascript code. It's the following:
public static function notReady() {
?>
<script>
if (!document.hidden) {
// Some operations...
}
</script>
<?php
}
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
?
I have a function inside a class which runs a javascript code. It's the following:
public static function notReady() {
?>
<script>
if (!document.hidden) {
// Some operations...
}
</script>
<?php
}
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 Answer
Reset to default 0Just 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
wp_head
but only do things on certain pages. Running the JS without hooking intowp_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 ♦ Commented Jan 11, 2021 at 14:16