Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Wp-rocket Lazyload

Modify attr when Lazyloading is activate on wp-rocket plugins

When lazy-loading is enabled (for example by WP Rocket), image URLs are stored in data-lazy-src / data-lazy-srcset attributes instead of src / srcset. You can tell the Weglot parser to detect and translate those attributes with the weglot_get_dom_checkers filter.

add_filter( 'weglot_get_dom_checkers', 'weglot_dom_check_data_lazy_src' );
function weglot_dom_check_data_lazy_src( $dom_checkers ) {

	if ( ! class_exists( 'Img_Data_Lazy_Src' ) ) {
		class Img_Data_Lazy_Src extends Weglot\Parser\Check\Dom\AbstractDomChecker {
			const DOM       = 'img';
			const PROPERTY  = 'data-lazy-src';
			const WORD_TYPE = Weglot\Parser\Definitions\Enum\WordType::IMG_SRC;
		}
	}
	$dom_checkers[] = '\Img_Data_Lazy_Src';

	if ( ! class_exists( 'Source_Data_Lazy_SrcSet' ) ) {
		class Source_Data_Lazy_SrcSet extends Weglot\Parser\Check\Dom\AbstractDomChecker {
			const DOM       = 'source';
			const PROPERTY  = 'data-lazy-srcset';
			const WORD_TYPE = Weglot\Parser\Definitions\Enum\WordType::IMG_SRC;
		}
	}
	$dom_checkers[] = '\Source_Data_Lazy_SrcSet';

	return $dom_checkers;
}

Add each class to $dom_checkers outside the class_exists() check: the filter can run several times in the same request (emails, PDF invoices, cart fragments), and the checker must be added every time.

Since Weglot 6.3 these classes live under Weglot\Parser\…. Snippets written with the older Weglot\Client\Api\Enum\WordType keep working.

Last updated