• Stars
    star
    107
  • Rank 323,587 (Top 7 %)
  • Language
    PHP
  • License
    GNU General Publi...
  • Created about 3 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

WordPress plugin for fluent management of scripts and styles.

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).

More Repositories

1

WordPress-Skeleton

Basic layout of a WordPress Git repository. I use this as a base when creating a new repo.
PHP
1,836
star
2

WP-Stack

A toolkit for creating professional WordPress deployments
PHP
1,113
star
3

WP-TLC-Transients

PHP
338
star
4

WordPress

This is legacy. Use github.com/WordPress/WordPress/ instead!
PHP
229
star
5

WordPress-Plugin-Directory-Slurper

Slurps down every stable version of every plugin in the WordPress plugin directory.
PHP
163
star
6

clerk-sveltekit

Clerk adapter for SvelteKit
Svelte
137
star
7

page-links-to

#WordPressPlugin: Lets you make a WordPress page (or other content type) link to an external URL of your choosing, instead of its WordPress URL.
PHP
106
star
8

wp-help

#WordPressPlugin: Site operators can create detailed, hierarchical documentation for the site's authors, editors, and contributors, viewable in the WordPress admin
PHP
104
star
9

cache-buddy

#WordPressPlugin: makes more WordPress pages cacheable
PHP
78
star
10

BetterBible

The Bible has some issues. Let's make it better.
72
star
11

Showdown

Example WordPress plugin created for WordCamp San Francisco 2014, to show off wp.Backbone.View
PHP
52
star
12

WordPress-Plugin-Readme-Parser

PHP
42
star
13

gifdrop

#WordPressPlugin GIF repository plugin for WordPress
PHP
36
star
14

Localdev

Simple Ruby command line utility to point domains to your localhost
Ruby
34
star
15

markdown-on-save

#WordPressPlugin: allows you to author posts in Markdown, but stores rendered HTML in post_content so you can deactivate with impunity.
PHP
29
star
16

slug-control

#WordPressPlugin: Helps you craft amazing post URL slugs, for that hand-crafted URL feel.
PHP
27
star
17

i-make-plugins

#WordPressPlugin: For plugin authors. Showcase your plugins on your WordPress site. You only update your readme.txt files!
PHP
19
star
18

WordPress-Jetpack-Valet-Driver

A Laravel Valet driver that routes missing images to your public site, via Jetpack's Site Accelerator (formerly "Photon") CDN.
PHP
18
star
19

wpsql

DEPRECATED. Use WP-CLI instead! Old: Bash script that, given a wp-config.php file, opens a MySQL session to the DB server described within
17
star
20

scheduled-post-guardian

#WordPressPlugin: Watches over scheduled posts, so that no funny business interferes with their mission
PHP
16
star
21

WordPress-Plugin-Installer

PHP
16
star
22

nice-search

#WordPressPlugin: Makes search URLs pretty
PHP
15
star
23

login-logo

#WordPressPlugin: Customize the logo on the WP login screen by simply dropping a file named login-logo.png into your WP content directory. CSS is automatic!
PHP
15
star
24

wp-feature-better-passwords

PHP
11
star
25

WPPlugin

Command line tool for updating and managing WordPress plugins
Ruby
11
star
26

gist-indexer

#INACTIVE: Use swdyh/gisty instead!
PHP
10
star
27

workerbee

A friendly tool for composing Cloudflare Workers
TypeScript
9
star
28

cmdy

Your friendly command manager
Shell
7
star
29

BlockStub

PHP
6
star
30

BashGato

Control Elgato lights with a Bash script
Shell
6
star
31

categorized

#WordPressPlugin: Unchecks the default post category when you select a non-default category
PHP
5
star
32

stabletweet

JavaScript
5
star
33

hotfix

Provides unofficial fixes for selected WordPress bugs, so you don't have to wait for the next WordPress core release.
PHP
5
star
34

Alfred-Silent-Mention

Alfred 4 Workflow to silently type @usernames, domains.com, or #hashtags without them being linked or parsed
3
star
35

sveltekit-playwright-fetch-mock

Tools for mocking SSR fetch() requests in SvelteKit for use in Playwright
TypeScript
2
star
36

WordPress-Skeleton-Updater

PHP
2
star
37

switch-controller

PHP
2
star
38

p3-neutraliser

WordPress plugin which prevent's Pipdig's P3 plugin from updating or requesting malicious URLs
PHP
2
star
39

grunt-wp-simple-plugin

Simple WordPress Plugin Template
CoffeeScript
2
star
40

dieulot.github.io

instantclick.io
JavaScript
1
star
41

grunt-puphpet-site

Initialize a PuPHPet site with grunt-init
Ruby
1
star
42

Wherewithal

Given constraints, parses a string of conditions into a valid MySQL WHERE clause
PHP
1
star
43

BigBrain

A familiar game to help you, uh, master your... mind.
Svelte
1
star
44

prefsync

Syncs macOS preferences
Shell
1
star