Download the PHP package pinkcrab/enqueue without Composer
On this page you can find all versions of the php package pinkcrab/enqueue. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download pinkcrab/enqueue
More information about pinkcrab/enqueue
Files in pinkcrab/enqueue
Package enqueue
Short Description A fluent API for enqueuing WordPress Scripts and Styles.
License MIT
Homepage https://pinkcrab.co.uk
Informations about the package enqueue
PinkCrab Enqueue
The PinkCrab Enqueue class allows for a clean and fluent alternative for enqueuing scripts and styles in WordPress.
To install
Version
Release 1.3.0
The above examples would enqueue the script and stylesheet using wp_enqueue_script() and wp_enqueue_style()
Features
Instantiation of \PinkCrab\Enqueue::class
You have 2 options when creating an instance of the Enqueue object.
When you call using the static methods script() or style(), the current instance is returned, allowing for chaining into a single call. Rather than doing it in the more verbose methods.
File Location
The URI to the defined js or css file can be defined here. This must be passed as a url and not the file path.
This is the same for both styles and scripts
Version
Like the underlying wp_enqueue_script() and wp_enqueue_style() function, we can define a verison number to our scripts. This can be done using the ver('1.2.2') method.
This is the same for both styles and scripts
However this can be frustrating while developing, so rather than using the current timestamp as a temp version. You can use the latest_version(), this grabs the last modified date from the defined script or style sheet, allowing reducing the frustrations of caching during development. While this is really handy during development, it should be changed to ->ver('1.2.2') when used in production.
This is the same for both styles and scripts
Dependencies
As with all wp_enqueue_script() and wp_enqueue_style() function, required dependencies can be called. This allows for your scripts and styles to be called in order.
This is the same for both styles and scripts
Localized Values
One of the most useful parts of of enqueuing scripts in WordPress is passing values form the server to your javascript files. Where as using the regular functions, this requires registering the style, localizing your data and then registering the script. While it works perfectly fine, it can be a bit on the verbose side.
The localize() method allows this all to be done within the single call.
This can only be called for scripts
Usage within js file (my-script.js)
Footer
By default all scripts are enqueued in the footer, but this can be changed if it needs to be called in the head. By calling either footer(false) or header()
This can only be called for scripts
Media
As with wp_enqueue_style() you can specify the media for which the sheet is defined for. Accepts all the same values as wp_enqueue_style()
This can only be called for styles
Attributes
It is possible (since v1.2.0) to add attributes and flags (value free attributes) to either script or style tags.
or
Async & Defer
There is also some shortcuts for making any script or style be deferred or async tagged.
or
Registration
Once your Enqueue object has been populated all you need to call register() for wp_enqueue_script() or wp_enqueue_style() to be called. You can either do all this inline (like the first example) or the Enqueue object can be populated and only called when required.
This is the same for both styles and scripts
Gutenberg
When registering scripts and styles for use with Gutenberg blocks, it is necessary to only register the assets before wp_enqueue_scripts
hook is called. To do this, all you need to is set for_block()
.
Public Methods
This obviously can be passed around between different classes/functions
Changelog
- 1.3.0 - Updated for php8, includes setting of custom script types, renamed lastest_version() to latest_version() and set deprecation notice.
- 1.2.1 : Now supports block use. If defined for block, scripts and styles will only be registered, not enqueued.
- 1.2.0 : Added in Attribute and Flag support with helpers for Aysnc and Defer
Contributions
If you would like to make any suggestions or contributions to this little class, please feel free to submit a pull request or reach out to speak to me. at [email protected].
WordPress Core Functions
This package uses the following wp core functions. To use PHP Scoper, please add the following functions. ['wp_enqueue_style', 'wp_register_script', 'wp_add_inline_script', 'wp_localize_script', 'wp_enqueue_script']