Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
fumikito committed Nov 30, 2020
1 parent 312a54d commit a6b7e9a
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.github/ export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.wp-env.json export-ignore
assets-lazy-loader.php export-ignore
package.json export-ignore
88 changes: 87 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,88 @@
# assets-lazy-loader
# Assets Lazy Loader

Lazy loader for WordPress theme.

## Installation

Use composer.

```
composer require kunoichi/assets-lazy-loader
```

## Usage

Enable each services 1 by 1 in your `functions.php`.

```php
// in your functions.php
require __DIR__ . '/vendor/autoload.php';
```

### ImageLazyLoader

Filter all `img` tag in your HTML and add `loading="lazy"` attributes. If the `img` tag already has `loading` attribute, no more attribute will be added.

```php
// Enable image lazy loader.
Kunoichi\AssetsLazyLoader\ImageLazyLoader::enable();
// If you want exclude some image(e.g. Featured image)
// a filter hook is available.
add_filter( 'assets_lazy_loader_image', function( $should, $tag ) {
return false !== strpos( $tag, 'size-post-thumbnail' );
}, 10, 2 );
```

### Deferred Scripts

Add `defer` attributes to JavaScripts enqueued with `wp_enqueue_script`.

```php
Kunoichi\AssetsLazyLoader\ScriptsDefer::enable( [
'exclude' => ['jquery-core'], // Only jQuery is not deferred.
'in_login' => true, // Add defer on login screen. Default false.
'in_admin' => true, // Same as above.
] );
```

### CSS Preload

Add `rel="preload"` to `link` tag and fallback scripts.

```php
Kunoichi\AssetsLazyLoader\StyleLoader::enable( [
'exclude' => StyleLoader::admin_critical( ['twentytwenty-style'] ), // Exclude default style and login/admin screen.
'in_login' => true,
'in_admin' => true,
] )
```

CSS preload caused non styled html in few seconds. To avoid shrinking of the screen by re-rendering, exclude critical css files from preload. In many case, it's the theme's main styelsheed.

`StyleLoader::admin_critical` is helpful for excluding ciritcal css in admin and login screen.

### jQuery Enhancement

The default jQuery bundled with WordPress has some issued.

- Version is old(1.12.4).
- Shipped with jQuery migrate unnecessory for sane plugins and themes.
- Enqueued in `head` tag.

You can assign other version of jQuery and drop `jquery-migrate`.

```php
JqueryOptimizer::enable( [
'footer' => true, // Move jQuery to footer.
'src' => 'https://code.jquery.com/jquery-3.5.1.slim.js', // Slim version from https://code.jquery.com/
'version' => '3.5.1', // Specify collect version.
] );
```

## Ackowledgements

- CSS preload depends on [fg-loadcss v2.1.0](https://www.npmjs.com/package/fg-loadcss/v/2.1.0) by Filament Group.

## License

GPL 3.0 or later.
2 changes: 0 additions & 2 deletions assets-lazy-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,5 @@
ImageLazyLoader::enable();
// If eye catch, they shouldn't be lazy.
add_filter( 'assets_lazy_loader_image', function( $should, $tag ) {
print_r( $tag );
exit;
return false !== strpos( $tag, 'size-post-thumbnail' );
}, 10, 2 );

0 comments on commit a6b7e9a

Please sign in to comment.