wp_enqueue_script() 方法可以安全地将 javascript 代码加入到 WordPress 创建的页面中。

如果调用的是自定义的 javascript  ,需要在使用前需要先通过 wp_register_script() 方法进行注册。

wp_enqueue_script() 描述


<?php wp_enqueue_script$handle$src$deps$ver$in_footer ); ?>See Notes for information about what action hooks should be used to call the function.

wp_enqueue_script() 用法



(string) (必填) Name used as a handle for the script. As a special case, if the string contains a '?' character, the preceding part of the string refers to the registered handle, and the succeeding part is appended to the URL as a query string. A version must be used with this special case.

默认值: None


(string) (可选) URL to the script, e.g. http://example.com/wp-content/themes/my-theme/my-theme-script.js. You should never hardcode URLs to local scripts. To get a proper URL to local scripts, use plugins_url() for plugins and get_template_directory_uri() for themes. Remote scripts can be specified with a protocol-agnostic URL, e.g. //otherdomain.com/js/their-script.js. This parameter is only required when the script with the given $handle has not been already registered using wp_register_script(). See Default Scripts Included and Registered by WordPress.

默认值: false


(array) (可选) Array of the handles of all the registered scripts that this script depends on, that is the scripts that must be loaded before this script. This parameter is only required when the script with the given $handle has not been already registered using wp_register_script(). Default handles are all in lower case.

默认值: array()


(string) (可选) String specifying the script version number, if it has one, which is concatenated to the end of the path as a query string. If no version is specified or set to false, then WordPress automatically adds a version number equal to the current version of WordPress you are running. If set to null no version is added. This parameter is used to ensure that the correct version is sent to the client regardless of caching, and so should be included if a version number is available and makes sense for the script.

默认值: false


(boolean) (可选) Normally, scripts are placed in <head> of the HTML document. If this parameter is true, the script is placed before the </body> end tag. This requires the theme to have the wp_footer() template tag in the appropriate place.

默认值: false

wp_enqueue_script() 参数



This function does not return a value.

wp_enqueue_script() 返回值


  • The function should be called using the wp_enqueue_scripts 动作 hook if you want to call it on the front-end of the site, like in the examples above. To call it on the administration screens, use the admin_enqueue_scripts 动作 hook. For the login screen, use the login_enqueue_scripts 动作 hook. Calling it outside of an 动作 hook can lead to problems, see the ticket #11526 for details.
  • Prior to Version 3.3, the function will have no effect if it is called using the wp_head or wp_print_scripts 动作 hooks or later, as this is too late to enqueue the files even if the $in_footer parameter is set to true.
  • As of Version 3.3, the function can be called mid-page (before the wp_footer() template tag) or using the wp_head 动作 hook. This will place the script in the footer as if the $in_footer parameter was set to true.
  • If you try to register or enqueue an already registered handle with different parameters, the new parameters will be ignored. Instead, use wp_deregister_script() and register the script again with the new parameters.
  • jQuery UI Effects is not included with the jquery-ui-core handle.
  • This function relies on the use of wp_head() and wp_footer() by the active theme. This means that it may not work with a few very old themes that do not call these functions. This is useful to keep in mind when debugging ancient themes.
  • 使用到: WP_Scripts::add(), WP_Scripts::add_data() and WP_Scripts::enqueue().
  • 使用到 global: (unknown type) $wp_scripts.

wp_enqueue_script() 注意


  • 添加于 版本: 2.6 (BackPress version: r16)

wp_enqueue_script() 历史


wp_enqueue_script() 函数的代码位于 wp-includes/functions.wp-scripts.php.

wp_enqueue_script() 源文件