PHPackages                             arraypress/wp-composer-assets - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. arraypress/wp-composer-assets

ActiveLibrary

arraypress/wp-composer-assets
=============================

Simple WordPress asset loading for Composer libraries with zero configuration

090↓100%PHP

Since Jan 16Pushed 3mo agoCompare

[ Source](https://github.com/arraypress/wp-composer-assets)[ Packagist](https://packagist.org/packages/arraypress/wp-composer-assets)[ RSS](/packages/arraypress-wp-composer-assets/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

WordPress Composer Assets
=========================

[](#wordpress-composer-assets)

Simple, WordPress-native asset loading for Composer libraries with zero configuration required. Automatically detects asset directories and handles WordPress-style enqueueing without any setup.

Features
--------

[](#features)

- 🚀 **Zero Configuration** - Just works with standard Composer library structures
- 📁 **Automatic Path Detection** - Finds your assets directory automatically
- 🎯 **WordPress-Native API** - Functions mirror `wp_enqueue_script()` exactly
- ⚡ **Performance Optimized** - Cached path resolution and optional explicit file API
- 🔧 **WP\_DEBUG Aware** - Debug logging only when needed
- 🏷️ **Auto Versioning** - File modification time for cache busting
- 🔄 **Mozart Compatible** - Works perfectly with prefixed libraries
- 📦 **PSR-4 Compliant** - Modern PHP standards

Installation
------------

[](#installation)

```
composer require arraypress/wp-composer-assets
```

Basic Usage
-----------

[](#basic-usage)

### Quick Start (Auto-Detection)

[](#quick-start-auto-detection)

```
// Enqueue a script - detects assets automatically
wp_enqueue_composer_script(
    'my-library-script',           // handle
    'js/script.js'                 // file path from assets/
);

// Enqueue a stylesheet
wp_enqueue_composer_style(
    'my-library-style',            // handle
    'css/style.css'                // file path from assets/
);
```

### Explicit File Reference (Recommended for Traits/Classes)

[](#explicit-file-reference-recommended-for-traitsclasses)

```
// Use __FILE__ for better performance and reliability
wp_enqueue_script_from_composer_file(
    'my-library-script',           // handle
    __FILE__,                      // calling file (__FILE__)
    'js/script.js',                // file path from assets/
    ['jquery'],                    // dependencies
    false,                         // version (false = auto-detect)
    true                           // in footer
);

wp_enqueue_style_from_composer_file(
    'my-library-style',            // handle
    __FILE__,                      // calling file (__FILE__)
    'css/style.css'                // file path from assets/
);
```

Directory Structure
-------------------

[](#directory-structure)

The library automatically detects these common Composer library patterns:

```
your-library/
├── assets/                    ← Assets here
│   ├── css/
│   │   └── style.css
│   └── js/
│       └── script.js
├── src/
│   ├── MyClass.php           ← Your PHP files here
│   └── Traits/
│       └── Assets.php
└── composer.json

```

Or deeper structures:

```
your-library/
├── assets/                    ← Assets here
└── src/
    └── Namespace/
        └── SubNamespace/
            └── MyClass.php    ← PHP files here

```

Advanced Usage
--------------

[](#advanced-usage)

### Object-Oriented Approach

[](#object-oriented-approach)

```
use ArrayPress\WP\ComposerAssets\AssetLoader;

// Use the class directly for more control
class MyLibraryAssets {

    public function enqueue_assets(): void {
        // Enqueue script with dependencies
        $success = AssetLoader::enqueue_script_from_file(
            'my-advanced-script',
            __FILE__,
            'js/advanced.js',
            ['jquery', 'wp-util'],
            '2.1.0',  // explicit version
            true
        );

        if ($success) {
            // Localize the script
            wp_localize_script('my-advanced-script', 'myLibraryData', [
                'ajaxUrl' => admin_url('admin-ajax.php'),
                'nonce'   => wp_create_nonce('my_library_nonce')
            ]);
        }

        // Enqueue conditional styles
        if (is_admin()) {
            AssetLoader::enqueue_style_from_file(
                'my-admin-style',
                __FILE__,
                'css/admin.css',
                ['wp-admin']
            );
        }
    }
}
```

### Get Asset URLs

[](#get-asset-urls)

```
// Get asset URL without enqueueing
$logo_url = wp_get_composer_asset_url('images/logo.png');

if ($logo_url) {
    echo '';
}
```

### WordPress Integration Example

[](#wordpress-integration-example)

```
class MyPlugin {

    public function __construct() {
        add_action('wp_enqueue_scripts', [$this, 'enqueue_frontend_assets']);
        add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_assets']);
    }

    public function enqueue_frontend_assets(): void {
        wp_enqueue_script_from_composer_file(
            'my-plugin-frontend',
            __FILE__,
            'js/frontend.js',
            ['jquery']
        );

        wp_enqueue_style_from_composer_file(
            'my-plugin-frontend',
            __FILE__,
            'css/frontend.css'
        );
    }

    public function enqueue_admin_assets(): void {
        wp_enqueue_script_from_composer_file(
            'my-plugin-admin',
            __FILE__,
            'js/admin.js',
            ['jquery', 'wp-util']
        );
    }
}
```

API Reference
-------------

[](#api-reference)

### Global Functions

[](#global-functions)

FunctionDescription`wp_enqueue_composer_script($handle, $file, $deps, $ver, $in_footer)`Enqueue script with auto-detection`wp_enqueue_composer_style($handle, $file, $deps, $ver, $media)`Enqueue style with auto-detection`wp_enqueue_script_from_composer_file($handle, $calling_file, $file, ...)`Enqueue script with explicit file`wp_enqueue_style_from_composer_file($handle, $calling_file, $file, ...)`Enqueue style with explicit file`wp_get_composer_asset_url($file)`Get asset URL without enqueueing### Class Methods

[](#class-methods)

MethodDescription`AssetLoader::enqueue_script($handle, $file, ...)`Class method for script enqueueing`AssetLoader::enqueue_style($handle, $file, ...)`Class method for style enqueueing`AssetLoader::get_asset_url($file)`Get asset URL`AssetLoader::get_debug_info()`Get debug information (WP\_DEBUG only)### Parameters

[](#parameters)

ParameterTypeDescription`$handle``string`WordPress asset handle (required)`$file``string`Path relative to assets/ directory`$calling_file``string`Usually `__FILE__` for explicit functions`$deps``array`Asset dependencies (default: `['jquery']` for scripts)`$ver``string|bool`Version string or `false` for auto-detection`$in_footer``bool`Load script in footer (default: `true`)`$media``string`CSS media type (default: `'all'`)Performance Tips
----------------

[](#performance-tips)

1. **Use explicit file functions** in traits and classes for better performance:

    ```
    // Faster - no debug_backtrace()
    wp_enqueue_script_from_composer_file('handle', __FILE__, 'js/file.js');

    // Slower - uses debug_backtrace()
    wp_enqueue_composer_script('handle', 'js/file.js');
    ```
2. **Cache handles** to avoid repeated calls:

    ```
    private static $assets_loaded = false;

    public function enqueue_assets() {
        if (self::$assets_loaded) return;

        wp_enqueue_script_from_composer_file('my-handle', __FILE__, 'js/script.js');
        self::$assets_loaded = true;
    }
    ```

Debugging
---------

[](#debugging)

Enable WordPress debug mode to see detailed logging:

```
// wp-config.php
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
```

Debug information will be logged to help troubleshoot asset loading issues.

Mozart Integration
------------------

[](#mozart-integration)

Works seamlessly with [Mozart](https://github.com/coenjacobs/mozart) for prefixed libraries:

```
// After Mozart prefixing, this still works perfectly
\Prefix\wp_enqueue_composer_script('my-handle', 'js/script.js');
```

Common Use Cases
----------------

[](#common-use-cases)

### WordPress Plugin Development

[](#wordpress-plugin-development)

```
class MyPlugin {
    public function __construct() {
        add_action('wp_enqueue_scripts', [$this, 'enqueue']);
    }

    public function enqueue() {
        wp_enqueue_script_from_composer_file(
            'my-plugin-main',
            __FILE__,
            'js/plugin.js'
        );
    }
}
```

### Library Development

[](#library-development)

```
trait AssetManager {
    protected function load_library_assets() {
        wp_enqueue_style_from_composer_file(
            'my-library-core',
            __FILE__,
            'css/library.css'
        );
    }
}
```

### Theme Development

[](#theme-development)

```
// functions.php
add_action('wp_enqueue_scripts', function() {
    wp_enqueue_script_from_composer_file(
        'theme-main',
        __FILE__,
        'js/theme.js',
        ['jquery']
    );
});
```

Requirements
------------

[](#requirements)

- **PHP:** 7.4 or higher
- **WordPress:** 5.0 or higher

Contributing
------------

[](#contributing)

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

License
-------

[](#license)

Licensed under the GPL-2.0+ License. See LICENSE file for details.

Support
-------

[](#support)

For support, please use the [issue tracker](https://github.com/arraypress/wp-composer-assets/issues).

###  Health Score

22

—

LowBetter than 23% of packages

Maintenance56

Moderate activity, may be stable

Popularity11

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity12

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/cd6eb8aff0903d87eb674d1ba3c5f3653899c0d7661504eb0deb7798ed86b643?d=identicon)[arraypress](/maintainers/arraypress)

---

Top Contributors

[![arraypress](https://avatars.githubusercontent.com/u/22668877?v=4)](https://github.com/arraypress "arraypress (10 commits)")

### Embed Badge

![Health badge](/badges/arraypress-wp-composer-assets/health.svg)

```
[![Health](https://phpackages.com/badges/arraypress-wp-composer-assets/health.svg)](https://phpackages.com/packages/arraypress-wp-composer-assets)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
