Quickly grasp the way to load JavaScript scripts in WordPress _javascript tips

Source: Internet
Author: User

In WordPress loaded script (for CSS and JS, the same below) file, most people's practice is directly in the header.php file to add link tag, or link tag through the Wp_head hook directly to the head tag, this practice is not in line with official Rules Fan's.

The standard script loading method should be the use of WordPress official functions provided (say), the unified standard has several benefits, the first is more secure and conducive to management, the second is more convenient and quick, and not only the theme can mount scripts, Plug-ins can also, and can also manage scripts, and adjust the Mount order and position and other content.

In which hook is loaded?

Before invoking the load script function, we first want to determine which hook the function executes at, and there are four commonly used scripts to load the hooks, namely wp_enqueue_scripts (loaded in the foreground), admin_enqueue_scripts (loaded in the background), Login_ Enqueue_scripts (loaded on login page) and init (global load).

The most commonly used should be the wp_enqueue_scripts, mount to this, the script is only loaded in the foreground.

Loading JavaScript Scripts

For example I want to load a JS in the foreground, is the topic root directory JS folder themes.js file. First you need to use the Wp_register_script () function to add JS to the script library (register the script), and then use the Wp_enqueue_script () function to mount the script.

function bing_enqueue_scripts () {
  wp_register_script (' Themes_js '), Get_bloginfo (' template_directory '). '/js/themes_js.js ');//Register JS script
  wp_enqueue_script (' Themes_js ') with ID Themes_js; Mount script
}
add_action (' Wp_ Enqueue_scripts ', ' bing_enqueue_scripts ');

Loading CSS Scripts

function bing_enqueue_scripts () {
  wp_register_style (' style '), Get_bloginfo (' template_directory '). '/style.css ');
  Wp_enqueue_style (' style ');
}
Add_action (' wp_enqueue_scripts ', ' bing_enqueue_scripts ');

Summary

This article is just very simple to explain how to load the script, reasonable use of the script queuing mechanism, can make the program flexible, also can speed up efficiency.

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.