wp_register_script()注册一个脚本

目录

描述

Registers a script file in WordPress to be linked to a page later using the wp_enqueue_script() function, which safely handles the script dependencies.

Scripts that have been pre-registered using wp_register_script() do not need to be manually enqueued using wp_enqueue_script() if they are listed as a dependency of another script that is enqueued. WordPress will automatically include the registered script before it includes the enqueued script that lists the registered script's handle as a dependency. See the Notes for more information.

wp_register_script() 描述

用法

<?php wp_register_script$handle$src$deps$ver$in_footer ); ?>See Notes for information about what action hooks should be used to call the function as well as some insights on the rationale for using this function.

wp_register_script() 用法

参数

$handle

(string) (必填) Name of the script. Should be unique as it is used as a handle for later use with wp_enqueue_script().

默认值: None

$src

(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.

默认值: None

$deps

(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. These scripts will automatically be enqueued when this script is enqueued with wp_enqueue_script(). Set false if there are no dependencies.

默认值: array()

$ver

(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

$in_footer

(boolean) (可选) Normally scripts are placed in the <head> section. If this parameter is true the script is placed at the bottom of the <body>. This requires the theme to have the wp_footer() hook in the appropriate place. Note that you have to enqueue your script before wp_head is run, even if it will be placed in the footer. (New in WordPress 2.8)

默认值: false

wp_register_script() 参数

返回值

(void

This function does not return a value.

wp_register_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. 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. Also see Notes on wp_enqueue_script() for more details about the proper hooks.
  • 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.
  • 使用到: WP_Scripts::add() and WP_Scripts::add_data().
  • 使用到 global: (unknown type) $wp_scripts.

Usage Rationale

  • Technically, you never have to register anything if you don't want to. The register functions are there and can be valuable if you want a central location that defines scripts and styles that will be used in your plugin/theme. You can then simply use the enqueue functions while referring to just the handle in order to enqueue the script or style for inclusion in the head.
  • If the handle of a registered script is listed in the $deps array of dependencies of another script that is enqueued with wp_enqueue_script(), that script will be automatically loaded prior to loading the enqueued script. This greatly simplifies the process of ensuring that a script has all the dependencies it needs. See below for a simple example.
  • So, the main purpose of the register functions is to allow you to simplify your code by removing the need to duplicate code if you enqueue the same script or style in more than one section of code. The benefits of this are many and probably don't need to be listed here.

Example of Automatic Dependency Loading

This example demonstrates how a registered script is automatically loaded when listed as the dependency of an enqueued script:

Note how, in the example above, `my-script.js` does not actually have to be enqueued. It is automatically loaded before `my-careers-script.js` by virtue of the fact that it was registered.

It's also interesting to note that, given how the `wp_enqueue_scripts` 动作 works, the order in which scripts are enqueued or registered is quite inconsequential. Really.

wp_register_script() 注意

历史

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

wp_register_script() 历史

源文件

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

wp_register_script() 源文件

相关