Complete the code to enable lazy loading for an image in WordPress.
<img src="image.jpg" loading="[1]" alt="Sample Image">
Setting loading="lazy" tells the browser to delay loading the image until it is near the viewport, improving page speed.
Complete the WordPress function to add lazy loading to images by filtering their HTML output.
function add_lazy_loading($html) {
return str_replace('<img', '<img loading="[1]"', $html);
}Replacing <img with <img loading="lazy" adds lazy loading to images in the HTML output.
Fix the error in the WordPress filter hook to enable lazy loading on images.
add_filter('the_content', '[1]'); function lazy_load_images($content) { return str_replace('<img', '<img loading="lazy"', $content); }
The filter expects the function name as a string without parentheses.
Fill both blanks to create a WordPress filter that adds lazy loading to images in post content.
add_filter('[1]', '[2]'); function add_lazy_loading($content) { return str_replace('<img', '<img loading="lazy"', $content); }
The filter hook is 'the_content' and the function name is 'add_lazy_loading'.
Fill all three blanks to create a WordPress shortcode that outputs an image with lazy loading.
function lazy_image_shortcode($atts) {
$atts = shortcode_atts(['src' => '', 'alt' => ''], $atts);
return '<img src="' . [1] . '" alt="' . [2] . '" loading="[3]">';
}
add_shortcode('lazyimg', 'lazy_image_shortcode');The shortcode uses the 'src' and 'alt' attributes from the shortcode attributes and sets loading to 'lazy' for lazy loading.