How to add JavaScript code to wordpress Web site

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.