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() 用法



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

默认值: 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.

默认值: None


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


(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 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() 参数



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:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

// Always use wp_enqueue_scripts 动作 hook to both enqueue and register scripts
add_action( 'wp_enqueue_scripts', 'enqueue_and_register_my_scripts' );

function enqueue_and_register_my_scripts(){

    // Use `get_stylesheet_directory_uri() if your script is inside your theme or child theme.
    wp_register_script( 'my-script', get_stylesheet_directory_uri() . '/js/my-script.js' );

    // Let's enqueue a script only to be used on a specific page of the site
    if ( is_page( 'careers' ) ){

        // Enqueue a script that has both jQuery (automatically registered by WordPress)
        // and my-script (registered earlier) as dependencies.
        wp_enqueue_script( 'my-careers-script', get_stylesheet_directory_uri() . '/js/my-careers-script.js', array( 'jquery', 'my-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.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
 * Register a new script.
 * Registers a script to be linked later using the wp_enqueue_script() function.
 * @see WP_Dependencies::add(), WP_Dependencies::add_data()
 * @since 2.6.0
 * @since 4.3.0 A return value was added.
 * @param string      $handle    Name of the script. Should be unique.
 * @param string      $src       Path to the script from the WordPress root directory. Example: '/js/myscript.js'.
 * @param array       $deps      Optional. An array of registered script handles this script depends on. Set to false if there
 *                               are no dependencies. Default empty array.
 * @param string|bool $ver       Optional. String specifying script version number, if it has one, which is concatenated
 *                               to end of path as a query string. If no version is specified or set to false, a version
 *                               number is automatically added equal to current installed WordPress version.
 *                               If set to null, no version is added. Default 'false'. Accepts 'false', 'null', or 'string'.
 * @param bool        $in_footer Optional. Whether to enqueue the script before  or before .
 *                               Default 'false'. Accepts 'false' or 'true'.
 * @return bool Whether the script has been registered. True on success, false on failure.
function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
	$wp_scripts = wp_scripts();
	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );

	$registered = $wp_scripts->add( $handle, $src, $deps, $ver );
	if ( $in_footer ) {
		$wp_scripts->add_data( $handle, 'group', 1 );

	return $registered;

wp_register_script() 源文件