Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How php references js code

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces php how to quote js code, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Where do I put the js code on wordpress?

In wordpress, js code is usually placed directly in a header.php or footer.php file to add js file calling code.

In fact, the best way for wordpress themes to reference js files is to use wordpress's built-in wp_enqueue_script () function, through which you can safely add javascript code to the pages created by WordPress, not only to maximize compatibility, but also to improve wordpress performance efficiency.

Function code:

Parameter description:

$handle-the name of the script that is called to distinguish other js, so it cannot be named the same as other js files.

The path to the $src-js file (that is, url). Do not use the domain name url directly, but use the path function, such as parent theme using get_template_directory_uri,child theme and get_stylesheet_directory_uri. (among the functions related to the path of the WP template, the one with template usually refers to parent theme, and the one with stylesheet points to child theme)

$deps-dependency, an array of identity strings (array:string) of other js on which the loaded js file depends, that is, the name of the code that needs to be loaded before this code (such as the js script depends on the jquery library, so here you want to write jquery in the form of an array), not required.

Ver-load the version number of the js file, which is appended at the end of the path as a query string, to ensure that the correct version information is passed to the client to avoid being affected by the cache (for example, when the js script changes, you can force the client browser to update the cache by changing the version number). The default is false, and the version number of the current wordpress program is called. If you do not want to display it, set it to NULL (not recommended).

The $in_footer-boolean type, which sets whether the js file call code is placed at the bottom of the html, ture at the bottom, and false at the head section. Prompt that you need the template to place the wp_footer () function correctly.

How to use it:

When you load a js file through the wp_enqueue_script function, you should assign it to a hook, such as calling it only in the foreground, using the hook wp_enqueue_script; only in the background, and using admin_enqueue_scripts.

Example:

Called only in the foreground

Called only in the background

Thank you for reading this article carefully. I hope the article "how to quote js code from php" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report