Explicit Fixed Width and Height when you have Elementor Carousels

Explicit Fixed Width and Height when you have Elementor Carousels and want to apply Elementor Optimization.

add_filter( ‘the_content’, ‘add_image_dimensions’ );

function add_image_dimensions( $content ) {
preg_match_all( ‘/]+>/i’, $content, $images );
if ( count( $images ) < 1 ) { return $content; }

foreach ( $images[0] as $image ) {
preg_match_all( ‘/(alt|title|src|width|class|id|height)=(“[^”]*”)/i’, $image, $img );
if ( ! in_array( ‘src’, $img[1] ) ) {
continue;
}

// Check if the image has the excluded classes
if ( in_array( ‘class’, $img[1] ) ) {
$classes = $img[2][ array_search( ‘class’, $img[1] ) ];
if ( preg_match( ‘/\b(elementor-widget-image-carousel|swiper|swiper-container)\b/i’, $classes ) ) {
continue;
}
}

if ( ! in_array( ‘width’, $img[1] ) || ! in_array( ‘height’, $img[1] ) ) {
$src = $img[2][ array_search( ‘src’, $img[1] ) ];

$alt = in_array( ‘alt’, $img[1] ) ? ‘ alt=’ . $img[2][ array_search( ‘alt’, $img[1] ) ] : ”;

$title = in_array( ‘title’, $img[1] ) ? ‘ title=’ . $img[2][ array_search( ‘title’, $img[1] ) ] : ”;

$class = in_array( ‘class’, $img[1] ) ? ‘ class=’ . $img[2][ array_search( ‘class’, $img[1] ) ] : ”;
$id = in_array( ‘id’, $img[1] ) ? ‘ id=’ . $img[2][ array_search( ‘id’, $img[1] ) ] : ”;

list( $width, $height, $type, $attr ) = getimagesize( str_replace( “\””, “”, $src ) );

$image_tag = sprintf( ‘‘, $src, $alt, $title, $class, $id, $width, $height );
$content = str_replace( $image, $image_tag, $content );
}
}

return $content;
}

Picture of Editors Team
Editors Team
Welcome to BigHungama.com, the ultimate hub for coding, learning, and innovation! We are a passionate team of educators, developers, and tech enthusiasts dedicated to making coding accessible, fun, and impactful for everyone.

Leave a Reply

Your email address will not be published. Required fields are marked *

Welcome to BigHungama.com, the ultimate hub for coding, learning, and innovation! We are a passionate team of educators, developers, and tech enthusiasts dedicated to making coding accessible, fun, and impactful for everyone.
Flag Counter