TailwindPHP
Plugins

Custom Plugins

A custom plugin is a PHP class implementing TailwindPHP\Plugin\PluginInterface. It receives a PluginAPI instance and uses it to register static and functional utilities, components, and variants. Once registered with registerPlugin(), the plugin resolves by name in a @plugin directive — the same mechanism the built-in typography and forms plugins use.

PluginInterface

Implement three methods:

namespace TailwindPHP\Plugin;

interface PluginInterface
{
    public function getName(): string;
    public function __invoke(PluginAPI $api, array $options = []): void;
    public function getThemeExtensions(array $options = []): array;
}
  • getName() returns the identifier used in @plugin "...".
  • __invoke() runs the plugin, registering everything through $api. The $options array carries the values from the @plugin CSS block.
  • getThemeExtensions() returns theme namespaces to merge before utilities are generated (return [] if none).

Example

use TailwindPHP\Plugin\PluginInterface;
use TailwindPHP\Plugin\PluginAPI;

class MyCustomPlugin implements PluginInterface
{
    public function getName(): string
    {
        return 'my-custom-plugin';
    }

    public function __invoke(PluginAPI $api, array $options = []): void
    {
        // Static utilities
        $api->addUtilities([
            '.btn' => [
                'padding' => '0.5rem 1rem',
                'border-radius' => '0.25rem',
                'font-weight' => '600',
            ],
            '.btn-primary' => [
                'background-color' => 'blue',
                'color' => 'white',
            ],
        ]);

        // Functional utilities (generates .tab-1, .tab-2, .tab-4, .tab-8)
        $api->matchUtilities(
            [
                'tab' => fn ($value) => ['tab-size' => $value],
            ],
            ['values' => ['1' => '1', '2' => '2', '4' => '4', '8' => '8']]
        );

        // Component classes (sorted into the components layer)
        $api->addComponents([
            '.card' => [
                'background-color' => 'white',
                'border-radius' => '0.5rem',
                'padding' => '1rem',
            ],
        ]);

        // Custom variant
        $api->addVariant('hocus', '&:hover, &:focus');

        // Read a theme value
        $primary = $api->theme('colors.blue.500', '#3b82f6');
    }

    public function getThemeExtensions(array $options = []): array
    {
        return []; // Return theme additions if needed
    }
}

PluginAPI reference

The API mirrors the TailwindCSS plugin API:

MethodSignaturePurpose
addBaseaddBase(array $css): voidAdd base-layer styles.
addUtilitiesaddUtilities(array $utilities, array $options = []): voidRegister static utility classes keyed by selector.
matchUtilitiesmatchUtilities(array $utilities, array $options = []): voidRegister functional utilities; $options['values'] supplies the value map, $options['supportsNegativeValues'] enables negatives.
addComponentsaddComponents(array $components, array $options = []): voidRegister component classes (components layer).
matchComponentsmatchComponents(array $components, array $options = []): voidRegister functional components with a value map.
addVariantaddVariant(string $name, string|array $variant): voidRegister a static variant from a selector.
matchVariantmatchVariant(string $name, callable $callback, array $options = []): voidRegister a functional variant; $options['values'] supplies the value map.
themetheme(string $path, mixed $defaultValue = null): mixedRead a theme value by dot path (e.g. colors.blue.500).
configconfig(?string $path = null, mixed $defaultValue = null): mixedRead a config value by dot path, or the whole config when $path is null.
prefixprefix(string $className): stringApply the configured prefix to a class name.

The callback passed to matchUtilities, matchComponents, and matchVariant receives the value and a second argument array containing a modifier key.

Registering and using

Register the plugin instance, then reference it by the name from getName():

use TailwindPHP\tw;
use function TailwindPHP\registerPlugin;

registerPlugin(new MyCustomPlugin());

$css = tw::generate(
    '<div class="btn btn-primary card tab-4">…</div>',
    '@plugin "my-custom-plugin"; @import "tailwindcss/utilities.css";'
);

See Plugins for the @plugin directive, and Theme for the values exposed through $api->theme().

On this page