This article introduces to you the content is about WordPress website add javascript code method, has certain reference value, has the need friend can refer to.
If you modify your own template theme, upgrade and update your own control, the addition of JavaScript code is more free, can be introduced according to the actual business logic. However, if you want to add a topic-independent JavaScript code, such as guest statistics, performance monitoring, and other types of standalone code , because it is not related to the theme file, then the best way is to add code in the plugin.
WordPress provides functions wp_enqueue_script and hooks wp_enqueue_scripts
To facilitate the developer's ability to embed custom code.
Here is a brief summary of the method steps added:
Create a new directory under the plugin directory, enter the directory and create a new plug-in file index.php, insert the following sample code:
<?php/*plugin name:custom Javascriptdescription:add Custom Javascript*/function add_custom_scripts () { wp_ Enqueue_script (' Jquery-ui ', ' https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js ', Array (), ' 1.12.1 ', false);} Add_action (' wp_enqueue_scripts ', ' add_custom_scripts ');
wp_enqueue_script function Five parameter description
1. string $handle #对当前引入的javascript文件的唯一命名
2. string $src = ' #javascript文档资源url
3. array $deps = Array () #依赖代码handle
4. String|bool|null $ver = False #版本号, if not specified, is consistent with the WordPress system version number
5. bool $in _footer = False #是否在文档底部, if False, the default is to load JavaScript at the head of the document
Finally save the file, go to the plugin center to activate the plugin, in the browser page to view the source code, you can see the introduction of JavaScript files.
Using the wp_enqueue_script function has the following advantages:
A. You can set the load order with other dependent code
B. You can specify a version number to facilitate cache management
C. You can specify the location of the embedded code in the document