Skip to content
This repository was archived by the owner on Dec 23, 2020. It is now read-only.

Commit dcc6a32

Browse files
committed
Update wp-lazy-loading.php
Add the suggested changes so this branch can be used as base for #10.
1 parent bf0f36b commit dcc6a32

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

wp-lazy-loading.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -119,31 +119,31 @@ function wp_lazy_loading_enabled( $tag_name, $context ) {
119119
return (bool) apply_filters( 'wp_lazy_loading_enabled', $default, $tag_name, $context );
120120
}
121121

122-
123-
// TODO: update docs.
124122
/**
125-
* Add `loading="lazy"` to `img` HTML tags if enabled.
123+
* Add `loading="lazy"` to `img` HTML tags.
126124
*
127125
* Currently the "loading" attribute is only supported for `img`, and is enabled by default.
128126
*
129127
* @since (TBD)
130128
*
131-
* @param string $content The raw post content to be filtered.
129+
* @param string $content The HTML content to be filtered.
132130
* @param string $context Optional. Additional context to pass to the filters. Defaults to `current_filter()` when not set.
133131
* @return string Converted content with 'loading' attributes added to images.
134132
*/
135133
function wp_add_lazy_load_attributes( $content, $context = null ) {
134+
if ( null === $context ) {
135+
$context = current_filter();
136+
}
137+
136138
if ( ! wp_lazy_loading_enabled( 'img', $context ) ) {
137139
return $content;
138140
}
139141

140142
return preg_replace_callback(
141-
'/<img(\s)[^>]+>/',
143+
'/<img\s[^>]+>/',
142144
function( array $matches ) {
143145
if ( ! preg_match( '/\sloading\s*=/', $matches[0] ) ) {
144-
$space = $matches[1];
145-
146-
return str_replace( '<img' . $space, '<img' . $space . 'loading="lazy" ', $matches[0] );
146+
return str_replace( '<img', '<img loading="lazy"', $matches[0] );
147147
}
148148

149149
return $matches[0];

0 commit comments

Comments
 (0)