{"id":19450862,"url":"https://github.com/morning-train/wp-enqueue","last_synced_at":"2025-06-15T00:33:27.573Z","repository":{"id":41384017,"uuid":"508712271","full_name":"Morning-Train/wp-enqueue","owner":"Morning-Train","description":"WP Enqueue","archived":false,"fork":false,"pushed_at":"2023-08-03T15:55:10.000Z","size":70,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-18T01:05:44.017Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Morning-Train.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-06-29T13:53:05.000Z","updated_at":"2023-07-18T02:11:09.000Z","dependencies_parsed_at":"2024-11-10T16:40:48.835Z","dependency_job_id":null,"html_url":"https://github.com/Morning-Train/wp-enqueue","commit_stats":{"total_commits":44,"total_committers":4,"mean_commits":11.0,"dds":"0.36363636363636365","last_synced_commit":"029c088f5b7c7c7481ab515604d236cc0a86b2af"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/Morning-Train/wp-enqueue","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morning-Train%2Fwp-enqueue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morning-Train%2Fwp-enqueue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morning-Train%2Fwp-enqueue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morning-Train%2Fwp-enqueue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Morning-Train","download_url":"https://codeload.github.com/Morning-Train/wp-enqueue/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morning-Train%2Fwp-enqueue/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259905403,"owners_count":22929916,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T16:39:19.290Z","updated_at":"2025-06-15T00:33:27.547Z","avatar_url":"https://github.com/Morning-Train.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# WP Enqueue\n\nFor easy script and style enqueueing in WordPress. With Laravel Mix Manifest and WP-Scripts asset file support!\n\n## Table of Contents\n\n- [Introduction](#introduction)\n- [Getting Started](#getting-started)\n    - [Installation](#installation)\n- [Usage](#usage)\n    - [Before you start](#before-you-start)\n    - [Defining the root Url](#defining-the-root-url)\n    - [Adding a MixManifest file](#adding-a-mixmanifest-file)\n    - [Loading scripts and styles](#loading-scripts-and-styles)\n        - [Enqueueing](#enqueueing)\n            - [Registering](#registering)\n    - [Options](#options)\n        - [Script](#script)\n        - [Style](#style)\n    - [Namespacing](#namespacing)\n- [Credits](#credits)\n- [Testing](#testing)\n- [License](#license)\n\n## Introduction\n\nThis tool is made for making the enqueueing and registering of WordPress scripts a bit more expressive and to make using\nlaravel Mix Manifest much easier to manage.\n\nThis tool lets you:\n\n- Define a root URL for your scripts and stylesheets so that you only have to use `get_stylesheet_directory_uri` once\n  and instead use relative paths when enqueueing\n- Add your `mix-manifest.json` file so that all mix compiled assets gets hashed automatically\n- Use a fluid interface for enqueueing assets\n\n## Getting Started\n\nFist [install](#installation) the package!\n\nThen from here on your entry point will be `\\Morningtrain\\WP\\Enqueue\\Enqueue`.\n\n### Installation\n\nThis tool is available as a package and can be installed through composer:\n\n```\ncomposer require morningtrain/wp-enqueue\n```\n\n## Usage\n\nHere is a quick example of how this package works!\n\n```php\nuse Morningtrain\\WP\\Enqueue\\Enqueue;\n\n// functions.php (or plugin.php)\nEnqueue::setup(get_stylesheet_directory_uri() . \"/public/build\", get_stylesheet_directory() . \"/public/build\");\n\n// Then wherever you wish to enqueue - preferably in the wp_enqueue_scripts action\nEnqueue::script('main')\n    -\u003esrc('js/main.js')\n    -\u003edeps('jquery')\n    -\u003eapplyAssetFile() // This applies the main.asset.php file containing dependencies and version. Dependencies are pushed to existing dependencies\n    -\u003eenqueue();\n\n// Or to simply register a stylesheet\nEnqueue::style('main')\n    -\u003esrc('css/main.css')\n    -\u003eregister();\n\n// In a block, on a route or in a condition somewhere you can now enqueue the already registered stylesheet\nEnqueue::style('main')-\u003eenqueue();\n```\n\n### Before you start\n\nAll relative paths should match paths in `webpack.mix.js`.\n\nSo if you have the following in your mix:\n\n```javascript\n// webpack.mix.js\n\nlet mix = require('laravel-mix');\n\nmix.js('resources/js/app.js', 'js').setPublicPath('public/build');\n```\n\nThen your public directory would be `public/build` and all assets would use a source relative to this path. So in the\nabove example, you would enqueue `app.js` like this:\n\n```php\nEnqueue::script('app', 'js/app.js')-\u003eenqueue();\n```\n\nNote: Enqueueing assets before the `wp_enqueue_scripts` hook will automatically delay the enqueueing until WordPress is\nready. You should, of course, still enqueue properly in the right hook.\n\n### Defining the root Url\n\nYou may define the root URL of your build directory.\n\nBy doing this you can now enqueue assets using a relative path. This should match the one defined in `webpack.mix.js` if\nyou are using [Laravel Mix](https://laravel-mix.com/)\n\n```php\n// Setting the root URL\n\\Morningtrain\\WP\\Enqueue\\Enqueue::setRootUrl(get_stylesheet_directory_uri() . '/public/build');\n```\n\nYou may also get the url by calling `Enqueue::getRootUrl()`\n\n```php\n// Getting the root URL\n$rootUrl = \\Morningtrain\\WP\\Enqueue\\Enqueue::getRootUrl();\n```\n\n### Adding a MixManifest file\n\nIf you are using [Laravel Mix](https://laravel-mix.com/) then you can add the generated `mix-manifest.json` file. By\ndoing this all enqueued assets will automatically use the hashed sources.\n\nThis is an easy and convenient way to clear client cached assets without worry.\n\n```php\n// Adding the manifest file\n\\Morningtrain\\WP\\Enqueue\\Enqueue::addManifest(get_stylesheet_directory() . '/public/build/mix-manifest.json');\n```\n\nYou may also retrieve the manifest content\n\n```php\n// Adding the manifest content\n\\Morningtrain\\WP\\Enqueue\\Enqueue::getManifest();\n```\n\n### Loading scripts and styles\n\nLoading a script or a style is almost the same!\nConstruct either a `Script` or a `Style` from `Enqueue`\n\nThen, using a fluid api, you can configure your asset and then either enqueue or register at the end.\n\nNote: These methods act the same as, and wraps, WordPress\nmethods [wp_enqueue_script()](https://developer.wordpress.org/reference/functions/wp_enqueue_script/)\nand [wp_enqueue_style()](https://developer.wordpress.org/reference/functions/wp_enqueue_style/) and their register\nequivalents.\n\n```php\n// Beginning an Enqueue chain\n// This is how you start enqueueing or registering a script\n\\Morningtrain\\WP\\Enqueue\\Enqueue::script('my-script');\n// ... and for a stylesheet\n\\Morningtrain\\WP\\Enqueue\\Enqueue::style('my-style');\n```\n\nAfter this inspect the instance returned. All options are available as chainable methods!\n\n#### Enqueueing\n\nTo enqueue simply end your chain by calling `enqueue()`\n\n```php\n// Enqueue a script called 'my-script' which is located in the /js directory\n\\Morningtrain\\WP\\Enqueue\\Enqueue::script('my-script')\n    -\u003esrc('js/my-script.js')\n    -\u003eenqueue();\n\n// Or you may supply the source as the second param as so\n\\Morningtrain\\WP\\Enqueue\\Enqueue::script('my-script', 'js/my-script.js')\n    -\u003eenqueue();\n```\n\n##### Registering\n\nTo register instead of enqueueing use `register()`\n\n```php\n// Register a script called 'my-script' which is located in the /js directory\n\\Morningtrain\\WP\\Enqueue\\Enqueue::script('my-script')\n    -\u003esrc('js/my-script.js')\n    -\u003eregister();\n```\n\nThen later you can enqueue your asset this way:\n\n```php\n// Enqueue a script called 'my-script' which has already been registered\n\\Morningtrain\\WP\\Enqueue\\Enqueue::script('my-script')\n    -\u003eenqueue();\n```\n\n### Options\n\nThere are the same options as the methods these classes wrap.\n\n**Note:** `deps()` also accepts a string and if you call it multiple times in the same chain then every call pushes its\nvalue to the list.\n\n#### Script\n\nHere is an example using all available options:\n\nSee [wp_enqueue_script](https://developer.wordpress.org/reference/functions/wp_enqueue_script/) on\ndeveloper.wordpress.org\n\n```php\n\\Morningtrain\\WP\\Enqueue\\Enqueue::script('my-script')\n    -\u003esrc('js/my-script.js')\n    -\u003edeps('jquery')\n    -\u003ever('1.0')\n    -\u003einFooter(true)\n    -\u003eenqueue();\n```\n\n#### Style\n\nHere is an example using all available options:\n\nSee [wp_enqueue_style](https://developer.wordpress.org/reference/functions/wp_enqueue_style/) on developer.wordpress.org\n\n```php\n\\Morningtrain\\WP\\Enqueue\\Enqueue::style('my-style')\n    -\u003esrc('css/my-style.css')\n    -\u003edeps('print-styles')\n    -\u003ever('1.0')\n    -\u003emedia('print')\n    -\u003eenqueue();\n```\n\n### Namespacing\n\nYou may register a namespace for a set of scripts or styles that live somewhere else in your codebase.\n\nTo do this simple add the namespace and then use this namespace in your handles. Namespacing is especially useful when\nwriting a plugin.\n\n#### Adding a namespace\n\n```php\n\\Morningtrain\\WP\\Enqueue\\Enqueue::addNamespace('myPlugin',\\plugin_dir_url(__FILE__). \"public/build\", __DIR__ . \"/public/build/mix-manifest.json\");\n```\n\n#### Using a namespace\n\n```php\n\\Morningtrain\\WP\\Enqueue\\Enqueue::style('myPlugin::main')\n    -\u003esrc('css/main.css')\n    -\u003eenqueue();\n```\n\n## Credits\n\n- [Mathias Munk](https://github.com/mrmoeg)\n- [All Contributors](../../contributors)\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorning-train%2Fwp-enqueue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmorning-train%2Fwp-enqueue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorning-train%2Fwp-enqueue/lists"}