Download the PHP package stellarwp/assets without Composer

On this page you can find all versions of the php package stellarwp/assets. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package assets

StellarWP Assets

Tests Static Analysis

A library for managing asset registration and enqueuing in WordPress.

Table of contents

Installation

It's recommended that you install Assets as a project dependency via Composer:

We actually recommend that this library gets included in your project using Strauss.

Luckily, adding Strauss to your composer.json is only slightly more complicated than adding a typical dependency, so checkout our strauss docs.

Notes on examples

Since the recommendation is to use Strauss to prefix this library's namespaces, all examples will be using the Boomshakalaka namespace prefix.

Configuration

This library requires some configuration before its features can be used. The configuration is done via the Config class.

Adding Group Paths

Now you can specify "group paths" in your application. This enables you to load assets which are stored in locations outside of your path set through Config::set_path( PATH_TO_YOUR_PROJECT_ROOT );

Note: Specifying the 4th parameter of add_group_path method as true, means that all the assets that belong to the specified group-path-slug will have their paths prefixed with css or js.

For example:

If you don't want the above to happen you would either specify false or leave the 4th parameter to its default state. Then the asset of the above example would be found in: GROUP_PATH_ROOT . 'group/path/relevant/path' . '/css/another.css'

Register and enqueue assets

There are a lot of options that are available for handling assets

Simple examples

For all examples, assume that the following use statement is being used:

A simple registration

A URL-based asset registration

Specifying the version

By default, assets inherit the version of that set in Config::get_version(), but you can specify a version manually:

Specifying a group path

To specify a group path first you need to have it registered. So in a hook prior your asset is being added to the group-path-slug you should run:

Then you can specify the above group path in your assets while being added or later.

Now the asset another-style would be search inside GROUP_PATH_ROOT . '/group/path/relevant/path'

Specifying the root path

By default, assets are searched for/found from the root path (unless they belong to a group path) of your project based on the value set in Config::get_path(), but you can specify a root path manually:

Priority of the Paths

  1. If a specific root path is set for the asset, that will be used.
  2. If a path group is set for the asset, that will be used.
  3. Otherwise, the root path of the project will be used.

Assets with no file extension

If you need to register an asset where the asset does not have an extension, you can do so by manually setting the asset type, like so:

Setting priority order

You can set scripts to enqueue in a specific order via the ::set_priority() method. This method takes an integer and works similar to the action/filter priorities in WP:

Dependencies

If your asset has dependencies, you can specify those like so:

You can also specify dependencies as a callable that returns an array of dependencies, like so:

Note that the callable will be executed when the asset is enqueued.

Auto-enqueuing on an action

To specify when to enqueue the asset, you can indicate it like so:

Adding JS and CSS at the same time

If you have a JS file and a CSS file that share the same directory and you wish to register them at the same time (typically helpful for assets built with wp-scripts), you can do this like so:

The following items get cloned over from the original asset:

Note: When auto-registering CSS or JS in this way, if there is a .asset.php file, the auto-registered asset will not use the .asset.php file. If there is an asset file for both, it is best to register each on their own, or to use ::clone_to(), make some changes, and then call ::use_asset_file( true ) on the cloned asset.

Comprehensive CSS example

The following example shows all of the options available during the registration of an asset.

Comprehensive JS example

Enqueuing manually

Sometimes you don't wish to set an asset to enqueue automatically on a specific action. In these cases, you can trigger a manual enqueue:

Enqueuing a whole group

If you have a group of assets that you want to enqueue, you can do so like this:

Working with registered Assets

exists()

You can check if an asset has been registered with this library by using the ::exists() method. This method takes the the asset slug as an argument and returns a bool.

get()

You can retrieve an asset object that has been registered by calling the ::get() method. This method takes the asset slug as an argument and returns an Asset object or null.

remove()

You can remove an asset from registration and enqueueing by calling the ::remove() method. This method takes the asset slug as an argument and returns an Asset object or null.

Advanced topics

Minified files

By default, if you register an asset and SCRIPT_DEBUG is not enabled, minified files will dynamically be used if present in the same directory as the original file. You can, however, specify a different path to look for the minified asset.

The following example will look for js/some-asset.min.js in src/assets/build/ (note the alteration of the file name):

Support for wp-scripts

This library supports *.asset.php files generated by wp-scripts out of the box. It will attempt to find the *.asset.php file in the same directory as the asset file you're registering, however, you can manually set the path to the asset file via ::set_asset_file() if you need to.

Default example

Assume you have a something.asset.php file in the same directory as your something.js file. Within that asset file is the standard asset array that contains dependencies and version keys.

This will automatically use the something.asset.php file's dependencies and version values for the asset.

Shown below is an example of the directory structure:

Within the something.asset.php file, you have the following:

Overriding the default asset file location

You may need to override the default location of the asset file. You can do this by using the ::set_asset_file() method.

Note: You can provide the JS file extension (other-asset-directory/something.js), the asset file extension (other-asset-directory/something.asset.php), or leave it off entirely (other-asset-directory/something).

Specifying translations for a JS asset

You can specify translations for a JS asset like so:

Conditional enqueuing

It is rare that you will want to enqueue an asset on every page load. Luckily, you can specify a condition for when an asset should be enqueued using the ::set_condition() method. This method takes a callable that should return a boolean that represents whether the asset should be enqueued or not.

Firing a callback after enqueuing occurs

Sometimes you need to know when enqueuing happens. You can specify a callback to be fired once enequeuing occurs using the ::call_after_enqueue() method. Like the ::set_condition() method, this method takes a callable.

Output JS data

If you wish to output JS data to the page after enqueuing (similar to wp_localize_script()), you can make use of the ::add_localize_script() method. This method takes two arguments: the first is the name of the JS variable to be output and the second argument is the data to be assigned to the JS variable. You can chain this method as many times as you wish!

If you specify an object name using dot notation, then the object will be printed on the page "merging" it with other, pre-existing objects. In the following example, the boomshakalaka.project object will be created and then the firstScriptData and secondScriptData objects will be added to it:

The resulting output will be:

Note the my-second-script-mod handle is overriding a specific nested key, boomshakalaka.project.secondScriptData.animal, in the boomshakalaka.project.secondScriptData object while preserving the other keys.

Using a callable to provide localization data

If you need to provide localization data dynamically, you can use a callable to do so. The callable will be called when the asset is enqueued and the return value will be used. The callable will be passed the asset as the first argument and should return an array.

Any valid callable can be used, including Closures, like in the example above.

Output content before/after a JS asset is output

There may be times when you wish to output markup or text immediately before or immediately after outputting the JS asset. You can make use of ::print_before() and ::print_after() to do this.

Style meta data

Assets support adding meta data to stylesheets. This is done via the ::add_style_data() method. This method takes two arguments: the first is the name of the meta data and the second is the value of the meta data. You can chain this and call this method multiple times.

This works similar to the wp_style_add_data() function.


All versions of assets with dependencies

PHP Build Version
Package Version
Requires php Version >=7.4
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package stellarwp/assets contains the following files

Loading the files please wait ....