Complete the code to add image alt text in WordPress.
add_image_size( 'custom-size', 800, 600, [1] );
The third parameter controls cropping. Setting it to true crops the image to exact dimensions.
Complete the code to enable lazy loading for images in WordPress.
add_filter( 'wp_lazy_loading_enabled', [1] );
Setting the filter to true enables lazy loading for images.
Fix the error in the code to resize an image on upload.
function resize_uploaded_image( $image ) {
$resized = wp_get_image_editor( $image[ 'file' ] );
if ( ! [1] ) {
return $image;
}
$resized->resize( 1024, 768, true );
$resized->save( $image[ 'file' ] );
return $image;
}The wp_get_image_editor returns false on failure, so check explicitly for === false.
Fill both blanks to create a filter that compresses JPEG images on upload.
add_filter( 'jpeg_quality', function( $quality ) { return [1]; } , [2] );
JPEG quality is set to 75 for good compression. The priority 90 ensures the filter runs late.
Fill all three blanks to create a shortcode that outputs an optimized image with alt text.
function optimized_image_shortcode( $atts ) {
$atts = shortcode_atts( [
'src' => '',
'alt' => '',
'size' => 'medium'
], $atts );
return wp_get_attachment_image( [1], [2], false, [ 'alt' => [3] ] );
}
add_shortcode( 'opt_image', 'optimized_image_shortcode' );The shortcode uses src for the image ID, size for image size, and alt for alt text.