AI Verified

Name

Explicit Fixed Width and Height Images

About

test

Language

PHP

Rating

Voted: 1 by 1 user(s)

Codevault

WebSquadron

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.1

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

18/08/2023

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Potential vulnerability found : Cross Site Scripting
Found on line : 29
Code : printf( '<img src=%s%s%s%s%s width="%d" height="%d" />', $src, $alt, $title, $class, $id, $width, $height )
Vulnerable line : 24
Code : $src = $img[2][ array_search( 'src', $img[1] ) ]);

Potential vulnerability found : Cross Site Scripting
Found on line : 29
Code : printf( '<img src=%s%s%s%s%s width="%d" height="%d" />', $src, $alt, $title, $class, $id, $width, $height )
Vulnerable line : 25
Code : $alt = in_array( 'alt', $img[1] ) ? ' alt=' . $img[2][ array_search( 'alt', $img[1] ) ] : '');

Potential vulnerability found : Cross Site Scripting
Found on line : 29
Code : printf( '<img src=%s%s%s%s%s width="%d" height="%d" />', $src, $alt, $title, $class, $id, $width, $height )
Vulnerable line : 26
Code : $title = in_array( 'title', $img[1] ) ? ' title=' . $img[2][ array_search( 'title', $img[1] ) ] : '');

Potential vulnerability found : Cross Site Scripting
Found on line : 29
Code : printf( '<img src=%s%s%s%s%s width="%d" height="%d" />', $src, $alt, $title, $class, $id, $width, $height )
Vulnerable line : 27
Code : $class = in_array( 'class', $img[1] ) ? ' class=' . $img[2][ array_search( 'class', $img[1] ) ] : '');

Potential vulnerability found : Cross Site Scripting
Found on line : 29
Code : printf( '<img src=%s%s%s%s%s width="%d" height="%d" />', $src, $alt, $title, $class, $id, $width, $height )
Vulnerable line : 28
Code : $id = in_array( 'id', $img[1] ) ? ' id=' . $img[2][ array_search( 'id', $img[1] ) ] : '');

Found 5 vulnerabilities

Explicit Fixed Width and Height Images

 
                    
1add_filter( 'the_content', 'add_image_dimensions' );
2 
3function add_image_dimensions( $content ) {
4 preg_match_all( '/<img[^>]+>/i', $content, $images );
5 if ( count( $images ) < 1 ) {
6 return $content;
7 }
8 
9 foreach ( $images[0] as $image ) {
10 preg_match_all( '/(alt|title|src|width|class|id|height)=("[^"]*")/i', $image, $img );
11 if ( ! in_array( 'src', $img[1] ) ) {
12 continue;
13 }
14 
15 // Check if the image has the excluded classes
16 if ( in_array( 'class', $img[1] ) ) {
17 $classes = $img[2][ array_search( 'class', $img[1] ) ];
18 if ( preg_match( '/\b(elementor-widget-image-carousel|swiper|swiper-container)\b/i', $classes ) ) {
19 continue;
20 }
21 }
22 
23 if ( ! in_array( 'width', $img[1] ) || ! in_array( 'height', $img[1] ) ) {
24 $src = $img[2][ array_search( 'src', $img[1] ) ];
25 $alt = in_array( 'alt', $img[1] ) ? ' alt=' . $img[2][ array_search( 'alt', $img[1] ) ] : '';
26 $title = in_array( 'title', $img[1] ) ? ' title=' . $img[2][ array_search( 'title', $img[1] ) ] : '';
27 $class = in_array( 'class', $img[1] ) ? ' class=' . $img[2][ array_search( 'class', $img[1] ) ] : '';
28 $id = in_array( 'id', $img[1] ) ? ' id=' . $img[2][ array_search( 'id', $img[1] ) ] : '';
29 list( $width, $height, $type, $attr ) = getimagesize( str_replace( "\"", "", $src ) );
30 $image_tag = sprintf( '<img src=%s%s%s%s%s width="%d" height="%d" />', $src, $alt, $title, $class, $id, $width, $height );
31 $content = str_replace( $image, $image_tag, $content );
32 }
33 }
34 
35 return $content;
36}

1

Related Snippets

Please see some snippets below related to this snippet..

Performance

AI Verified

0

Deactivate Specific Plugins on Staging & Non-Production Sites [SnipSnip.pro]

Added: 5 months ago

Last Updated: 5 months ago

This "Deactivate_Plugins_On_Staging" script selectively deactivates specified plugins on your staging and non-production environments based on the site URL. Ideal for disabling plugins that are only n...

Performance

AI Verified

1

Ensure Webfont is Loaded (Non-Elementor)

Added: 1 year ago

Last Updated: 3 months ago

Ensure Webfont is loaded

Performance

AI Verified

2

WP Cache Buster

Added: 1 year ago

Last Updated: 10 months ago

Force design changes to apply to all existing website visitors with browser caching on. Details, code and idea from this post https://www.recolize.com/en/blog/wordpress-cache-busting-design-changes/

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

Performance

AI Verified

32

Remove Unused Javascript

Added: 1 year ago

Last Updated: 3 days ago

Remove Unused Javascript - and improve your Page Speed Insight Score

WooCommerce

Pro Verified

10

Deactivate some WooCommerce Checkout Fields

Added: 1 year ago

Last Updated: 2 weeks ago

Deactivate some WooCommerce Checkout Fields from showing

Elementor

AI Verified

6

CSS Grid Aid

Added: 8 months ago

Last Updated: 1 month ago

This can be used for any WordPress Builder to aid working with CSS Grids.