I've created a WordPress plugin that inserts width
and height
attributes to all images, for example:
<img src="https://placehold.it/100x50">
- to -
<img src="https://placehold.it/100x50" width="100" height="50">
Yet, when there are images that have blank dimensions (for both width
and height
), my code doesn't doesn't catch it, for example:
<img src="https://placehold.it/100x50" width="" height="">
However, when there is only one blank dimension, my plugin does catch it. The following two examples will work:
<img src="https://placehold.it/100x50" width="">
<img src="https://placehold.it/100x50" height="">
Here's the source code. On line 43 (#), this is the logic I'm using to determine if an image is missing a width
/height
:
if ( ! in_array( 'width', $img[1] ) || ! in_array( 'height', $img[1] ) ) {
I've tried the following updated logic, but it doesn't work:
if ( # Images with no width/height
! in_array( 'width', $img[1] ) || ! in_array( 'height', $img[1] ) ||
# Images with blank width/height
( in_array( 'width', $img[1] ) && in_array( '""', $img[1] ) ) || ( in_array( 'height', $img[1] ) && in_array( '""', $img[1] ) )
) {
Am I in the right area? What am I doing wrong in my logic?