functions – How to add height and width dimensions automatically to archive descriptions

Question

I have this code which adds height and width data for images in posts and pages:

function add_img_size($content){
  $pattern = '/<img [^>]*?src="(https?:\/\/[^"]+?)"[^>]*?>/iu';
  preg_match_all($pattern, $content, $imgs);
  foreach ( $imgs[0] as $i => $img ) {
    if ( false !== strpos( $img, 'width=" ) && false !== strpos( $img, "height=" ) ) {
      continue;
    }
    $img_url = $imgs[1][$i];
    $img_size = @getimagesize( $img_url );
      
    if ( false === $img_size ) {
      continue;
    }
    $replaced_img = str_replace( "<img ', '<img ' . $img_size[3] . ' ', $imgs[0][$i] );
    $content = str_replace( $img, $replaced_img, $content );
  }
  return $content;
}
add_filter('the_content','add_img_size');

However, this code does not add in height and width data for any images that are present in tag descriptions or category descriptions.

Question: how can I edit this code so that tag descriptions and category descriptions are also included in the filter/function above and images in tag/category descriptions have height and width data added automatically?

Thanks.

0
Tom Tolkien 2 months 2022-12-18T09:21:21-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse