Encute

0

Encute provides a fluent, declarative API for site owners to manipulate the scripts and styles that WordPress, themes, and plugins shove onto their site. Move things into the footer, de

Version
Last updated
Active installations
WordPress Version
Tested up to
PHP Version
Rating
Total ratings
Tag
This plugin is outdated and might not be supported anymore.

Description

Encute provides a fluent, declarative API for site owners to manipulate the scripts and styles that WordPress, themes, and plugins shove onto their site. Move things into the footer, defer loading, remove assets entirely. Or load scripts async, or as modules, or as nomodule!

Here’s an example of how you could use the plugin:

<?php

use CWSEncute{ Plugin, Script, Style };

add_action(Plugin::class, function (Plugin $plugin) {
    $isContactPage = fn () => is_page('contact');
    Script::get('contact-form-7')->keepIf($isContactPage)->footer()->defer();
    Style::get('contact-form-7')->keepIf($isContactPage)->footer()->defer();

    Style::get(['mediaelement', 'wp-mediaelement'])->footer()->defer();
    Style::get('material-icons')->defer();
    Script::get('jquery')->remove();
});

Wrapper

Always run code in this wrapper:

add_action(CWSEncutePlugin::class, function (CWSEncutePlugin $encute) {
    // Your code here.
});

This wrapper will be a no-op if Encute is not available, and it will both wait for Encute to be available to run, and pass you Encute’s main class instance.

Fluency

Both Script::get() and Style::get() return an instance of themselves, as do all calls to their methods, so you can just chain your calls.

Script

  • static CWSEncuteScript::get(string $handle): CWSEncuteScript — get a Script instance for that handle.
  • CWSEncuteScript::module(): CWSEncuteScript — make the script a module.
  • CWSEncuteScript::noModule(): CWSEncuteScript — make the script nomodule.
  • CWSEncuteScript::footer(): CWSEncuteScript — send the script to the footer (along with its entire dependency family).
  • CWSEncuteScript::async(): CWSEncuteScript — make the script async.
  • CWSEncuteScript::defer(): CWSEncuteScript — make the script defer.
  • CWSEncuteScript::remove(): CWSEncuteScript — remove the script.
  • CWSEncuteScript::removeIf(callable $callback): CWSEncuteScript — remove the script if the callback resolves as true.
  • CWSEncuteScript::keepIf(callable $callback): CWSEncuteScript — keep the script if the callback resolves as true (else remove it).

Style

  • static CWSEncuteStyle::get(string $handle): CWSEncuteStyle — get a Style instance for that handle.
  • CWSEncuteStyle::footer(): CWSEncuteStyle — send the style to the footer (along with its entire dependency family).
  • CWSEncuteStyle::defer(): CWSEncuteStyle — defer the style’s loading.
  • CWSEncuteStyle::remove(): CWSEncuteStyle — remove the style.
  • CWSEncuteStyle::removeIf(callable $callback): CWSEncuteStyle — remove the style if the callback resolves as true.
  • CWSEncuteStyle::keepIf(callable $callback): CWSEncuteStyle — keep the style if the callback resolves as true (else remove it).