wp_enqueue_scripts
云策文档标注
概述
wp_enqueue_scripts 是一个 WordPress 动作钩子,用于在前端页面中正确排队加载脚本和样式。尽管名称中包含“scripts”,但它同时适用于脚本和样式。
关键要点
- wp_enqueue_scripts 是前端脚本和样式排队的标准钩子,应优先使用。
- 对于管理页面,应使用 admin_enqueue_scripts;对于登录页面,应使用 login_enqueue_scripts。
- 注意 do_blocks() 在 wp_enqueue_scripts 之前调用,可能导致脚本注册问题,需条件检查。
代码示例
function wpdocs_theme_name_scripts() {
wp_enqueue_style( 'style-name', get_stylesheet_uri() );
wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'wpdocs_theme_name_scripts' );注意事项
- wp_enqueue_scripts 钩子不传递 $hook 参数,这与 admin_enqueue_scripts 不同。
- 在内容触发(如短码)中排队脚本时,需确保脚本已注册,避免因 do_blocks() 调用顺序导致的错误。
原文内容
Fires when scripts and styles are enqueued.
Source
do_action( 'wp_enqueue_scripts' );
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |
Skip to note 7 content
Drew Jaynes
Basic Example
/** * Proper way to enqueue scripts and styles. */ function wpdocs_theme_name_scripts() { wp_enqueue_style( 'style-name', get_stylesheet_uri() ); wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true ); } add_action( 'wp_enqueue_scripts', 'wpdocs_theme_name_scripts' );Skip to note 8 content
jon
Instead of this Action, use ‘admin_enqueue_scripts’ for Admin pages and ‘login_enqueue_scripts’ for the login page.
Skip to note 9 content
thejaydip
If you want to add dynamic inline styles.
function wpdocs_enqueue_custom_style() { $theme = wp_get_theme(); wp_register_style( 'wpdocs-style', get_theme_file_uri( 'css/style.css' ), array(), $theme->get( 'Version' ), ); wp_enqueue_style( 'wpdocs-style' ); $custom_css = ".navbar-nav ul li { list-style: none; }"; wp_add_inline_style( 'wpdocs-style', $custom_css ); } add_action( 'wp_enqueue_scripts', 'wpdocs_enqueue_custom_style' );Skip to note 10 content
Chigozie Orunta
Selectively load JS files into specific pages like so:
function wpdocs_selective_js_loading() { if ( is_page( ['home', 'about', 'contact'] ) ) { wp_enqueue_script( 'your-script', get_template_directory_uri() . '/js/your-script.js', array(), '1.0.0', true ); } } add_action( 'wp_enqueue_scripts', 'wpdocs_selective_js_loading' );Skip to note 11 content
tripflex
do_blocks()is called beforewp_enqueue_scriptsso if you are correctly registering scripts and only enqueuing on pages it’s needed on (which most developers do not seem to do), keep in mind that if you use something likewp_localize_script, it will not work if you’re registering your script inwp_enqueue_scriptshook and enqueueing it from some kind of trigger generated by content, shortcode, template files, or something else that could be loaded bydo_blocks.This will result in javascript errors saying
XYZ variable is not definedI had a lot of plugin clients report issues because of this, as my call to
wp_enqueue_scriptis normally triggered by a template file or something that now gets loaded indo_blocks()(like a shortcode), which is BEFOREwp_enqueue_scriptsaction is triggered, meaning the script is not registered yet.The solution for me in this situation was to create a conditional check before calling
wp_localize_scriptand thenwp_enqueue_script, to see if the script has been registered already, and if not, make sure to callwp_register_scriptfirst.Skip to note 12 content
abdessamad idrissi
This actions passes an argument
$hookthat is handy when for example to prevent the script from loading on certain pages;function wpdocs_enqueue_scripts( $hook ) { // Load only in add new post page if ( is_admin() && 'post-new.php' !== $hook ) { return; } // rest of your code here.. } add_action( 'wp_enqueue_scripts', 'wpdocs_enqueue_scripts' );wp_enqueue_scriptsaction, however, it does apply for theadmin_enqueue_scriptsaction.