Assets
Include frontend assets to customize your plugin's appearance and behavior.
Declaring Assets
In plugin.json:
{
"assets": {
"css": [
"css/styles.css",
"css/admin.css"
],
"js": [
"js/script.js",
"js/admin.js"
],
"img": [
"images/logo.svg",
"images/icon.png"
]
}
}Asset Directory Structure
Publishing Assets
Assets are automatically published when plugin is enabled:
Manual republish:
Force republish (overwrites existing):
Using Assets in Templates
Example CSS
Example JavaScript
Modern JavaScript (ES6+)
CSS Best Practices
Prefix your classes with plugin name:
Don't modify global styles:
Use consistent spacing:
Responsive design:
JavaScript Best Practices
Use IIFE to avoid global pollution:
Wait for DOM ready:
Handle async operations:
Event delegation:
Asset Optimization
For production:
Minify CSS and JavaScript
Optimize images (compress, use appropriate formats)
Use CDNs for common libraries
Bundle assets if using multiple files
Cache busting with versioned filenames
Loading External Libraries
If you need external libraries (jQuery, etc.):
Option 1: CDN
Option 2: Composer dependency Add to plugin's composer.json and include in your asset bundle.
Option 3: Bundle with plugin Include library file in assets/js/vendor/ and declare in plugin.json.
Asset Path in Plugin
Published assets location:
Related Guides
Plugin Manifest - Declare assets
UI Components - Style widgets and tabs
Translations - Multi-language JavaScript
Last updated