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

  1. Prefix your classes with plugin name:

  1. Don't modify global styles:

  1. Use consistent spacing:

  1. Responsive design:

JavaScript Best Practices

  1. Use IIFE to avoid global pollution:

  1. Wait for DOM ready:

  1. Handle async operations:

  1. Event delegation:

Asset Optimization

For production:

  1. Minify CSS and JavaScript

  2. Optimize images (compress, use appropriate formats)

  3. Use CDNs for common libraries

  4. Bundle assets if using multiple files

  5. 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:

Last updated