A Laravel Mix function for WordPress themes.
The mix function is useful if you want to enable cache busting for your theme asset files (CSS, JavaScript, images, icon sprites). Laravel Mix allows you to create a mix-manifest.json
file, which might look like this:
{
"/css/styles.css": "/css/styles.css?id=6ed48b0b831e80bd7549",
"/js/scripts.js": "/js/scripts.js?id=1bdd07b944e933aa88aa",
}
The ID parameter is a hash of the file contents that changes every time that you make a change to a file. The mix()
function provided in this package allows you to use these hashed URLs for enqueueing your assets in your WordPress theme.
You can install the package via Composer:
composer require mindkomm/theme-lib-mix
The mix function assumes that you have a mix-manifest.json
(generated by the version function of Laravel Mix) in the build
folder of your theme.
add_action( 'wp_enqueue_scripts', function() {
wp_enqueue_style(
'styles',
mix( 'build/css/styles.css' )
);
} );
If the mix function can’t find your asset file in the manifest file, it will return the asset URL through get_theme_file_uri
as a fallback.
Name | Summary | Type | Returns/Description |
---|---|---|---|
mix | Get the path to a versioned Mix file. | string |
The file URL. |
Gets the path to a versioned Mix file.
Inspired by https://www.sitepoint.com/use-laravel-mix-non-laravel-projects/
mix( string $path, string $manifest_directory = build )
Returns: string
The file URL.
Name | Type | Description |
---|---|---|
$path | string |
The relative path to the file. |
$manifest_directory | string |
Optional. Custom path to manifest directory. Default 'build'. |
This is a library that we use at MIND to develop WordPress themes. You’re free to use it, but currently, we don’t provide any support.