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.