WordPress plugin for fluent management of scripts and styles.

Overview

Encute

WordPress plugin for fluent management of scripts and styles.

Managing scripts and styles on the front of your site can be tricky. This makes it trivial to do stuff like:

  • Shove a script (and its entire connected family) into the footer
  • Defer a script or a style until page load
  • Load a script async
  • Mark a script as type="module"
  • Mark a script as nomodule
  • Remove some plugin script or style you don't need

Installation

If you're using composer you can composer require markjaquith/encute. Else, install via Git.

Usage

Put this in an mu-plugins drop-in:

<?php

use CWS\Encute\Plugin;
use CWS\Encute\Script;
use CWS\Encute\Style;

add_action(Plugin::class, function (Plugin $encute) {
	$encute->debug(); // Optional: Adds HTML comments to scripts and styles, making it easier to see the handle.

	// Move jQuery to footer and defer its loading.
	Script::get('jquery')->footer()->defer();

	// Move 'some-handle' to the footer.
	Script::get('some-handle')->footer();

	// Defer 'wp-embed'.
	Script::get('wp-embed')->defer();
	
	// Make 'some-module' load as a module.
	Script::get('some-module')->module();
	
	// Make 'nomodule-fallback' load as nomodule.
	Script::get('nomodule-fallback')->noModule();
	
	// Move 'admin-bar' styles to the footer and defer their loading.
	Style::get('admin-bar')->footer()->defer();

	// Move 'wp-block-library' styles to the footer.
	Style::get('wp-block-library')->footer();

	// Keep 'contact-form-7' style on contact page only.
	Style::get('contact-form-7')->keepIf(fn() => is_page('contact'));

	// Remove 'cruft' script on the about page.
	Script::get('cruft')->removeIf(fn() => is_page('about'));
});

API

Initialization

Always run code in this wrapper:

add_action(\CWS\Encute\Plugin::class, function (\CWS\Encute\Plugin $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

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 CWS\Encute\Script::get(string $handle): CWS\Encute\Script — get a Script instance for that handle.
  • CWS\Encute\Script::module(): CWS\Encute\Script — make the script a module.
  • CWS\Encute\Script::noModule(): CWS\Encute\Script — make the script nomodule.
  • CWS\Encute\Script::footer(): CWS\Encute\Script — send the script to the footer (along with its entire dependency family).
  • CWS\Encute\Script::async(): CWS\Encute\Script — make the script async.
  • CWS\Encute\Script::defer(): CWS\Encute\Script — make the script defer.
  • CWS\Encute\Script::remove(): CWS\Encute\Script — remove the script.
  • CWS\Encute\Script::removeIf(callable $callback): CWS\Encute\Script — remove the script if the callback resolves as true.
  • CWS\Encute\Script::keepIf(callable $callback): CWS\Encute\Script — keep the script if the callback resolves as true (else remove it).

Style

  • static CWS\Encute\Style::get(string $handle): CWS\Encute\Style — get a Style instance for that handle.
  • CWS\Encute\Style::footer(): CWS\Encute\Style — send the style to the footer (along with its entire dependency family).
  • CWS\Encute\Style::defer(): CWS\Encute\Style — defer the style's loading.
  • CWS\Encute\Style::remove(): CWS\Encute\Style — remove the style.
  • CWS\Encute\Style::removeIf(callable $callback): CWS\Encute\Style — remove the style if the callback resolves as true.
  • CWS\Encute\Style::keepIf(callable $callback): CWS\Encute\Style — keep the style if the callback resolves as true (else remove it).
You might also like...
Easy handle APlayer on WordPress. A shortcode for WordPress to using APlayer.

Description Easy handle APlayer on WordPress. A shortcode for WordPress to using APlayer. Support [audio] tag, compatible with AMP. Requirement WordPr

WordPress & TypeScript. Simple starter template for WordPress projects

WordPress & TypeScript. Simple starter template for WordPress projects that want to use TypeScript in combination with @wordpress/scripts

A PHP client for Wordpress websites that closely implement the XML-RPC WordPress API

Wordpress XML-RPC PHP Client A PHP client for Wordpress websites that closely implement the XML-RPC WordPress API Created by Hieu Le MIT licensed. Cur

Beauty and simple Wordpress video player plugin. Powerfull and lite in use.
Beauty and simple Wordpress video player plugin. Powerfull and lite in use.

Sonic Spectre Video Player Beauty and simple Wordpress video player plugin. Powerfull and lite in use. Quick Start: Download plugin from this repo and

Wordless is a junction between a WordPress plugin and a theme boilerplate that dramatically speeds up and enhances your custom theme creation
Wordless is a junction between a WordPress plugin and a theme boilerplate that dramatically speeds up and enhances your custom theme creation

Wordless is a junction between a WordPress plugin and a theme boilerplate that dramatically speeds up and enhances your custom theme creation. Some of

 WordPress plugin people can use to embed to their website:  a Mintbase NEAR NFT  and  Mintbase NEAR store
WordPress plugin people can use to embed to their website: a Mintbase NEAR NFT and Mintbase NEAR store

mintbase-embed WordPress plugin people can use to embed to their website: a Mintbase NEAR NFT and Mintbase NEAR store This is demo plugin that allows

WPCloudDeploy is a WordPress plugin that allows you to easily deploy servers at major cloud-server providers and then install apps
WPCloudDeploy is a WordPress plugin that allows you to easily deploy servers at major cloud-server providers and then install apps

WPCloudDeploy is a WordPress plugin that allows you to easily deploy servers at major cloud-server providers and then install apps

A WordPress plugin for managing user permission, education, and communication.

A WordPress plugin for managing user permission, education, and communication. Texas A&M University System

A WordPress plugin for crawling information from the Iranian customs site and displaying it in Elementor
A WordPress plugin for crawling information from the Iranian customs site and displaying it in Elementor

A WordPress plugin for crawling information from the Iranian customs site and displaying it in Elementor

Owner
Mark Jaquith
I make things on the web.
Mark Jaquith
Querycase provides a convenient, fluent interface for creating and running database queries in WordPress.

Querycase database for WordPress Dependency-free library to create SQL Queries in WordPress. Explore the documentation → ℹ️ About Querycase Querycase

Alessandro Tesoro 7 Oct 17, 2021
A WordPress plugin to suspend WordPress sites automagically. Simple and lightweight, no annoying ads and fancy settings.

Suspend WP A WordPress plugin to suspend WordPress sites automagically. Simple and lightweight, no annoying ads and fancy settings. ?? Demo (coming so

Waren Gonzaga 3 Nov 15, 2021
Simple WordPress plugin to learn how to understand WordPress Crons and the Action Scheduler library.

Simple WordPress plugin to learn how to understand WordPress Crons and the Action Scheduler library. Import Jamendo playlists with tracks in WordPress posts.

Pierre Saikali 3 Dec 7, 2022
The Pronamic WordPress Basecone plugin allows you to connect your WordPress installation to Basecone.

Pronamic WordPress Basecone The Pronamic WordPress Basecone plugin allows you to connect your WordPress installation to Basecone. Table of contents Au

Pronamic 1 Oct 19, 2021
WordPress plugin that lets you use Discourse as the community engine for a WordPress blog

WP Discourse Note: the wp-discourse plugin requires >= PHP-5.4.0. The WP Discourse plugin acts as an interface between your WordPress site and your Di

Discourse 497 Dec 10, 2022
This WordPress Plugin Boilerplate is meant for you to develop your own plugin on.

WordPress Plugin Boilerplate This plugin boilerplate is meant for you to develop your own plugin on. Support & collaboration Features OOP plugin core

richardev 2 May 10, 2022
A curated list of Awesome WordPress Theme, Plugins and Framework development Resources and WordPress Communities.

Awesome WordPress A curated list of Awesome WordPress Theme, Plugins and Framework development Resources and WordPress Communities. Inspired by bayand

Dropndot Limited 91 Dec 26, 2022
🚀WordPress Plugin Boilerplate using modern web techs like TypeScript, SASS, and so on... on top of a local development environment with Docker and predefined GitLab CI for continous integration and deployment!

WP React Starter: WordPress React Boilerplate DEPRECATED: WP React Starter was a "research project" of devowl.io for the development of our WordPress

devowl.io GmbH 344 Jan 1, 2023
A custom WordPress nav walker class to fully implement the Twitter Bootstrap 4.0+ navigation style (v3-branch available for Bootstrap 3) in a custom theme using the WordPress built in menu manager.

WP Bootstrap Navwalker This code in the main repo branch is undergoing a big shakeup to bring it in line with recent standards and to merge and test t

WP Bootstrap 3.3k Jan 5, 2023
Twenty Twenty-Two, the default WordPress theme that will launch with WordPress 5.9.

Twenty Twenty-Two Welcome to the development repository for the default theme that will launch with WordPress 5.9. About Twenty Twenty-Two is designed

null 414 Nov 28, 2022