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.
Download stellarwp/assets
More information about stellarwp/assets
Files in stellarwp/assets
Package assets
Short Description A library for managing asset registration and enqueuing in WordPress.
License GPL-2.0
Informations about the package assets
StellarWP Assets
A library for managing asset registration and enqueuing in WordPress.
Table of contents
- Installation
- Notes on examples
- Configuration *Adding Group Paths
- Register and enqueue assets
- Simple examples
- A simple registration
- A URL-based asset registration
- Specifying the version
- Specifying a group path
- Specifying the root path
- Priority of the Paths
- Assets with no file extension
- Dependencies
- Auto-enqueuing on an action
- Adding JS and CSS at the same time
- Comprehensive CSS example
- Comprehensive JS example
- Enqueuing manually
- Enqueuing a whole group
- Working with registered
Assets
exists()
get()
remove()
- Advanced topics
- Minified files
- Support for wp-scripts
- Default example
- Overriding the default asset file location
- Specifying translations for a JS asset
- Conditional enqueuing
- Firing a callback after enqueuing occurs
- Output JS data
- Using a callable to provide localization data
- Output content before/after a JS asset is output
- Style meta data
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
- If a specific root path is set for the asset, that will be used.
- If a path group is set for the asset, that will be used.
- 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:
add_to_group()
enqueue_on()
set_condition()
set_min_path()
set_path()
- Asset slug (
-style
,-script
,-css
, and-js
are stripped from the end of the original asset slug and replaced with either-css
or-js
) - Version
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.