wpseek.com
Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren



wp_enqueue_script › WordPress Function

Seit2.1.0
Veraltetn/v
› wp_enqueue_script ( $handle, $src = '', $deps = array(), $ver = false, $args = array() )
Parameter: (5)
  • (string) $handle Name of the script. Should be unique.
    Erforderlich: Ja
  • (string) $src Full URL of the script, or path of the script relative to the WordPress root directory. Default empty.
    Erforderlich: Nein
    Standard: (leer)
  • (string[]) $deps Optional. An array of registered script handles this script depends on. Default empty array.
    Erforderlich: Nein
    Standard: 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.
    Erforderlich: Nein
    Standard: false
  • (array|bool) $args { Optional. An array of extra args for the script. Default empty array. Otherwise, it may be a boolean in which case it determines whether the script is printed in the footer. Default false. @type string $strategy Optional. If provided, may be either 'defer' or 'async'. @type bool $in_footer Optional. Whether to print the script in the footer. Default 'false'. @type string $fetchpriority Optional. The fetch priority for the script. Default 'auto'. @type array $module_dependencies Optional. IDs for module dependencies loaded via dynamic import. Default empty array. For the full data format, see the `$deps` param of \wp_register_script_module. When provided, the script must either be printed in the footer (with `in_footer` set to true) or use a deferred loading `strategy` (`defer`), so that the script modules import map is printed before the script is evaluated. Otherwise dynamic imports may fail to resolve. The translations of these script modules are printed after the scripts in the footer. A script that is not deferred should import them once the document has been parsed, so that their translations are available. }
    Erforderlich: Nein
    Standard: array()
Siehe:
  • WP_Dependencies::add()
  • WP_Dependencies::add_data()
  • WP_Dependencies::enqueue()
Definiert in:
Codex:
Changelog:
  • 6.3.0
  • 6.9.0
  • 7.0.0

Enqueues a script.

Registers the script if $src provided (does NOT overwrite), and enqueues it.


Quellcode

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

	$wp_scripts = wp_scripts();

	if ( $src || ! empty( $args ) ) {
		/** @var array{ 0: non-empty-string, 1?: string } $_handle */
		$_handle = explode( '?', $handle );
		if ( ! is_array( $args ) ) {
			$args = array(
				'in_footer' => (bool) $args,
			);
		}

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

	$wp_scripts->enqueue( $handle );
}