WP_Dependencies::add() WordPress Method

The WP_Dependencies::add() method is used to add a new dependency to the WordPress dependencies list. This is useful for plugins and themes that need to load additional files or dependencies. The first parameter is the handle of the new dependency, the second is the path to the file or dependency, and the third is an array of dependencies that the new dependency needs.

WP_Dependencies::add( string $handle, string|bool $src, string[] $deps = array(), string|bool|null $ver = false, mixed $args = null ) #

Register an item.


Description

Registers the item if no item of that name already exists.


Top ↑

Parameters

$handle

(string)(Required)Name of the item. Should be unique.

$src

(string|bool)(Required)Full URL of the item, or path of the item relative to the WordPress root directory. If source is set to false, item is an alias of other items it depends on.

$deps

(string[])(Optional) An array of registered item handles this item depends on.

Default value: array()

$ver

(string|bool|null)(Optional) String specifying item 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.

Default value: false

$args

(mixed)(Optional) Custom property of the item. NOT the class property $args. Examples: $media, $in_footer.

Default value: null


Top ↑

Return

(bool) Whether the item has been registered. True on success, false on failure.


Top ↑

Source

File: wp-includes/class.wp-dependencies.php

	public function add( $handle, $src, $deps = array(), $ver = false, $args = null ) {
		if ( isset( $this->registered[ $handle ] ) ) {
			return false;
		}
		$this->registered[ $handle ] = new _WP_Dependency( $handle, $src, $deps, $ver, $args );

		// If the item was enqueued before the details were registered, enqueue it now.
		if ( array_key_exists( $handle, $this->queued_before_register ) ) {
			if ( ! is_null( $this->queued_before_register[ $handle ] ) ) {
				$this->enqueue( $handle . '?' . $this->queued_before_register[ $handle ] );
			} else {
				$this->enqueue( $handle );
			}

			unset( $this->queued_before_register[ $handle ] );
		}

		return true;
	}


Top ↑

Changelog

Changelog
VersionDescription
2.6.0Moved from WP_Scripts.
2.1.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.