ralfhortt/wp-assets

A helper package for working with the WordPress assets

2.2.2 2024-03-24 21:26 UTC

This package is auto-updated.

Last update: 2024-04-24 21:43:11 UTC


README

Installation

composer require ralfhortt/wp-assets

Documentation

Scripts

<?php
use \RalfHortt\Assets\Script;
use \RalfHortt\Assets\AdminScript;
use \RalfHortt\Assets\EditorScript;
use \RalfHortt\Assets\LoginScript;

new Script(string $handle, string $source, array $dependencies = [], string $version = true, bool $autoload = true);
new AdminScript(string $handle, string $source, array $dependencies = [], string $version = true, bool $autoload = true);
new EditorScript(string $handle, string $source, array $dependencies = [], string $version = true, bool $autoload = true);
new LoginScript(string $handle, string $source, array $dependencies = [], string $version = true, bool $autoload = true);

InlineScripts

use \RalfHortt\Assets\InlineScript;

new InlineScript(string $handle, string $data, bool $after = true);

Styles

<?php
use \RalfHortt\Assets\Style;
use \RalfHortt\Assets\AdminStyle;
use \RalfHortt\Assets\EditorStyle;
use \RalfHortt\Assets\LoginStyle;

new Style(string $handle, string $source, array $dependencies = [], $version = true, bool $autoload = true);
new AdminStyle(string $handle, string $source, array $dependencies = [], $version = true, bool $autoload = true);
new EditorStyle(string $handle, string $source, array $dependencies = [], $version = true, bool $autoload = true);
new LoginStyle(string $handle, string $source, array $dependencies = [], $version = true, bool $autoload = true);
new PrintStyle(string $handle, string $source, array $dependencies = [], $version = true, bool $autoload = true);

Usage

<?php
// Initialize the Style object
$myTheme = new Style(get_stylesheet_directory_uri() . '/my-theme', 'theme.css');

// Hook Style object into WordPress lifecycle
$myTheme->register();

// InlineScript
$inlineScript = new InlineScript('my-theme', 'const ThemeName = "Awesome"', true);
$inlineScript->register();

// or
(new Style('my-theme', 'theme.css', ['global.css'], '1.0.0', true))->register();
<?php
// Shortversion
(new Style(get_stylesheet_directory_uri() . '/my-theme', 'theme.css'))->register();
<?php
// Reuse asset
(new Style('my-theme', 'theme.css')->register();
(new LoginStyle('my-theme'))->register();