Download the PHP package morningtrain/wp-enqueue without Composer
On this page you can find all versions of the php package morningtrain/wp-enqueue. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download morningtrain/wp-enqueue
More information about morningtrain/wp-enqueue
Files in morningtrain/wp-enqueue
Package wp-enqueue
Short Description A tool for enqueueing assets in WordPress with support for Laravel Mix manifest
License MIT
Informations about the package wp-enqueue
WP Enqueue
For easy script and style enqueueing in WordPress. With Laravel Mix Manifest and WP-Scripts asset file support!
Table of Contents
- Introduction
- Getting Started
- Installation
- Usage
- Before you start
- Defining the root Url
- Adding a MixManifest file
- Loading scripts and styles
- Enqueueing
- Registering
- Enqueueing
- Options
- Script
- Style
- Namespacing
- Credits
- Testing
- License
Introduction
This tool is made for making the enqueueing and registering of WordPress scripts a bit more expressive and to make using laravel Mix Manifest much easier to manage.
This tool lets you:
- Define a root URL for your scripts and stylesheets so that you only have to use
get_stylesheet_directory_uri
once and instead use relative paths when enqueueing - Add your
mix-manifest.json
file so that all mix compiled assets gets hashed automatically - Use a fluid interface for enqueueing assets
Getting Started
Fist install the package!
Then from here on your entry point will be \Morningtrain\WP\Enqueue\Enqueue
.
Installation
This tool is available as a package and can be installed through composer:
Usage
Here is a quick example of how this package works!
Before you start
All relative paths should match paths in webpack.mix.js
.
So if you have the following in your mix:
Then your public directory would be public/build
and all assets would use a source relative to this path. So in the
above example, you would enqueue app.js
like this:
Note: Enqueueing assets before the wp_enqueue_scripts
hook will automatically delay the enqueueing until WordPress is
ready. You should, of course, still enqueue properly in the right hook.
Defining the root Url
You may define the root URL of your build directory.
By doing this you can now enqueue assets using a relative path. This should match the one defined in webpack.mix.js
if
you are using Laravel Mix
You may also get the url by calling Enqueue::getRootUrl()
Adding a MixManifest file
If you are using Laravel Mix then you can add the generated mix-manifest.json
file. By
doing this all enqueued assets will automatically use the hashed sources.
This is an easy and convenient way to clear client cached assets without worry.
You may also retrieve the manifest content
Loading scripts and styles
Loading a script or a style is almost the same!
Construct either a Script
or a Style
from Enqueue
Then, using a fluid api, you can configure your asset and then either enqueue or register at the end.
Note: These methods act the same as, and wraps, WordPress methods wp_enqueue_script() and wp_enqueue_style() and their register equivalents.
After this inspect the instance returned. All options are available as chainable methods!
Enqueueing
To enqueue simply end your chain by calling enqueue()
Registering
To register instead of enqueueing use register()
Then later you can enqueue your asset this way:
Options
There are the same options as the methods these classes wrap.
Note: deps()
also accepts a string and if you call it multiple times in the same chain then every call pushes its
value to the list.
Script
Here is an example using all available options:
See wp_enqueue_script on developer.wordpress.org
Style
Here is an example using all available options:
See wp_enqueue_style on developer.wordpress.org
Namespacing
You may register a namespace for a set of scripts or styles that live somewhere else in your codebase.
To do this simple add the namespace and then use this namespace in your handles. Namespacing is especially useful when writing a plugin.
Adding a namespace
Using a namespace
Credits
- Mathias Munk
- All Contributors
Testing
License
The MIT License (MIT). Please see License File for more information.