wp_enqueue_script

函数
wp_enqueue_script ( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false )
参数
  • (string) $handle Name of the script. Should be unique.
    Required:
  • (string) $src Full URL of the script, or path of the script relative to the WordPress root directory. Default empty.
    Required:
    Default: (empty)
  • (string[]) $deps Optional. An array of registered script handles this script depends on. Default empty array.
    Required:
    Default: array()
  • (string|bool|null) $ver Optional. String specifying script version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added.
    Required:
    Default: false
  • (bool) $in_footer Optional. Whether to enqueue the script before `</body>` instead of in the `<head>`. Default 'false'.
    Required:
    Default: false
相关
  • WP_Dependencies::add()
  • WP_Dependencies::add_data()
  • WP_Dependencies::enqueue()
定义位置
相关方法
wp_enqueue_scriptswp_dequeue_scriptwp_enqueue_stylewp_enqueue_editorenqueue_embed_scripts
引入
2.1.0
弃用
-

wp_enqueue_script: 这个函数用于在WordPress中排队等候一个脚本。脚本通常用于向网站添加动态行为,如互动元素或动画: 这个函数需要几个参数,包括脚本的句柄、脚本的源文件、脚本可能具有的任何依赖性,以及脚本是否应该在页脚加载。

排队等候脚本。

如果提供了$src,则注册该脚本(不覆盖),并排队等待它。

function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) {
	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );

	$wp_scripts = wp_scripts();

	if ( $src || $in_footer ) {
		$_handle = explode( '?', $handle );

		if ( $src ) {
			$wp_scripts->add( $_handle[0], $src, $deps, $ver );
		}

		if ( $in_footer ) {
			$wp_scripts->add_data( $_handle[0], 'group', 1 );
		}
	}

	$wp_scripts->enqueue( $handle );
}

常见问题

FAQs
查看更多 >