PHP code example of kunoichi / assets-lazy-loader

1. Go to this page and download the library: Download kunoichi/assets-lazy-loader library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kunoichi / assets-lazy-loader example snippets


// in your functions.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 );

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

Kunoichi\AssetsLazyLoader\ScriptsDefer::enable( [
	// Defer scripts only which you know they are safe with defer attribute.
	'exclude'  => [ 'your-js', 'jquery' ], 
] );

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

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