@@ -141,23 +141,30 @@ function wp_add_lazy_load_attributes( $content, $context = null ) {
141
141
'/<img\s[^>]+>/ ' ,
142
142
function ( array $ matches ) use ( $ content , $ context ) {
143
143
if ( ! preg_match ( '/\sloading\s*=/ ' , $ matches [0 ] ) ) {
144
- $ old_html = $ matches [0 ];
145
- $ new_html = str_replace ( '<img ' , '<img loading="lazy" ' , $ old_html );
144
+ $ tag_html = $ matches [0 ];
146
145
147
146
/**
148
- * Filters the tag HTML after adding the `loading` attribute .
147
+ * Filters the `loading` attribute value. Default `lazy` .
149
148
*
150
- * The variable part of the filter name is the filtered tag's name. For example for `img`
151
- * the filter would become `wp_add_lazy_loading_to_img` .
149
+ * Returning `false` or an empty string will not add the attribute.
150
+ * Returning `true` will add the default value .
152
151
*
153
152
* @since (TBD)
154
153
*
155
- * @param string $new_html The tag's HTML after adding the attribute .
156
- * @param string $old_html The tag's HTML before adding the attribute .
157
- * @param string $content The HTML content that is being filtered .
154
+ * @param string $default The filtered value, defaults to `lazy` .
155
+ * @param string $tag_html The tag's HTML.
156
+ * @param string $content The HTML containing the image tag .
158
157
* @param string $context Optional. Additional context. Defaults to `current_filter()`.
159
158
*/
160
- return apply_filters ( 'wp_add_lazy_loading_to_img ' , $ new_html , $ old_html , $ content , $ context );
159
+ $ value = apply_filters ( 'wp_set_image_loading_attr ' , 'lazy ' , $ tag_html , $ content , $ context );
160
+
161
+ if ( $ value ) {
162
+ if ( ! in_array ( $ value , array ( 'lazy ' , 'eager ' ), true ) ) {
163
+ $ value = 'lazy ' ;
164
+ }
165
+
166
+ return str_replace ( '<img ' , '<img loading=" ' . $ value . '" ' , $ tag_html );
167
+ }
161
168
}
162
169
163
170
return $ matches [0 ];
0 commit comments