最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Running javascript without hooking wp_head

programmeradmin0浏览0评论

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?

Share Improve this question asked Jan 11, 2021 at 13:55 H. M..H. M.. 1135 bronze badges 1
  • 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 Commented Jan 11, 2021 at 14:16
Add a comment  | 

1 Answer 1

Reset to default 0

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

发布评论

评论列表(0)

  1. 暂无评论