PHP code example of brain / assets
1. Go to this page and download the library: Download brain/assets library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
brain / assets example snippets
use Brain\Assets\Assets;
// For plugins.
// First param is main plugin file,second optional param
// is the subfolder in which assets files are saved.
Assets::forPlugin(__FILE__, '/dist');
// For themes.
// First param is the subfolder in which assets files are saved.
Assets::forTheme('/dist');
// For child themes.
// First param is the subfolder in which assets files are saved.
Assets::forChildTheme('/dist');
// For libraries.
// First param is the name of the library.
// Second param is absolute path in which assets files are saved.
// Third param is the absolute URL that points to the base path.
Assets::forLibrary('my-lib', __DIR__ . '/dist', content_url('/vendor/acme/my-lib/dist'));
$assets = Assets::forPlugin(__FILE__, '/dist');
$assets->enqueueScript('my-script', strategy: 'async')
->useAsync()
->useAttribute('crossorigin', 'anonymous')
->localize('MyScriptData', ['foo' => 'bar'])
->prependInline("window.foo = 'Foo';");
$assets->enqueueStyle('my-alt-style')
->asAlternate()
->useAttribute("disabled", null)
->useAttribute("data-something", "Something")
->withTitle('my style')
->withCondition('lte IE 10')
->appendInline(".custom-color: #{$customColor}");
$assets = Assets::forTheme('/dist')->useDependencyExtractionData();
$assets->enqueueScript('main', strategy: 'async');
$assets->enqueueScript('secondary', strategy: Strategy::newDeferInFooter());
$assets->enqueueScript('head', strategy: Strategy::newInHead());
wp_enqueue_script(
'my-theme-main',
'https://example.com/wp-content/themes/my-theme/dist/main.js?v=a29c9d677e174811e603',
['react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data'],
null,
['in_footer' => false, 'strategy' => 'async']
);
wp_enqueue_script(
'my-theme-secondary',
'https://example.com/wp-content/themes/my-theme/dist/secondary.js?v=d1e2af1f57008411b820',
['wp-api-fetch', 'lodash'],
null,
['in_footer' => true, 'strategy' => 'defer']
);
wp_enqueue_script(
'my-theme-head',
'https://example.com/wp-content/themes/my-theme/dist/head.js?v=2933858c47af52e33b8a',
[],
null,
['in_footer' => false]
);
$myAssets = Assets::forTheme('/dist')->useDependencyExtractionData()->registerAllFromManifest();
add_action('admin_enqueue_scripts'), fn () => $myAssets->keep('*-admin')->enqueue());
add_action('wp_enqueue_scripts'), fn () => $myAssets->keep('*-view')->enqueue());
$myAssets = Assets::forTheme('/dist')->useDependencyExtractionData()->registerAllFromManifest();
$myAssets->byName('main', 'js')->localize('MyData', $mainScriptData);
add_action('wp_enqueue_scripts'), fn () => $myAssets->discard('*-admin')->enqueue());
$assets = Assets::forTheme('/dist')->useDependencyExtractionData();
foreach (glob($assets->context()->basePath() . '*.{css,js}', GLOB_BRACE) as $file) {
str_ends_with($file, '.css')
? $assets->registerStyle(basename($file, '.css'))
: $assets->registerScript(basename($file, '.js'));
}
add_action('admin_enqueue_scripts'), fn () => $assets->collection()->keep('*-admin')->enqueue());
add_action('wp_enqueue_scripts'), fn () => $assets->collection()->keep('*-view')->enqueue());
Assets::forPlugin(__FILE__, '/dist')
->tryMinUrls()
->enqueueStyle('my-style.css');
Assets::forPlugin(__FILE__, '/dist')->enqueueStyle('my-style.min');
$assets->enqueueExternalScript('foo-js', 'https://cdn.example.com/foo.js?v=1.0')
->useDefer()
->useAttribute('data-id', 'foo-script')
->withCondition('lte IE 10')
->localize('MyScriptData', ['foo' => 'bar'])
->prependInline("window.foo = 'Foo';");
->appendInline("delete window.foo;");
$assets = Assets::forPlugin(__FILE__, '/dist');
$styleUrl = $assets->assetUrl('css/my-style.css');
$scriptUrl = $assets->assetUrl('js/my-script.js');
$imageUrl = $assets->assetUrl('images/foo.jpg');
$fontUrl = $assets->assetUrl('fonts/bar.eot');
$videoUrl = $assets->assetUrl('videos/baz.mp4');
$assets = Assets::forPlugin(__FILE__, '/dist')
->withCssFolder('/css')
->withJsFolder('/js')
->withImagesFolder('/images')
->withVideosFolder('/videos')
->withFontsFolder('fonts');
$styleUrl = $assets->cssUrl('my-style');
$scriptUrl = $assets->jsUrl('my-script');
$imageUrl = $assets->imgUrl('foo.jpg');
$fontUrl = $assets->fontUrl('bar.eot');
$videoUrl = $assets->videoUrl('baz.mp4');
print $assets->cssUrl('my-style');
// https://www.example.com/wp-content/themes/my-theme/dist/my-style.css?v=1708973312
$assets->rawAssetUrl('my-style.css');
$assets->rawCssUrl('my-style');
$assets->rawJsUrl('my-script');
$assets->rawImgUrl('foo.jpg');
$assets->rawFontUrl('bar.eot');
$assets->rawVideoUrl('baz.mp4');
print $assets->dontAddVersion()->cssUrl('my-style');
// https://www.example.com/wp-content/themes/my-theme/dist/my-style.css