WordPress: rimuovere gli attributi width ed height dalle immagini

Come rimuovere gli attributi HTML width ed height inseriti da WordPress sulle immagini.

WordPress di Gabriele Romanato

A tutte le immagini inserite tramite la Media Gallery o l'uploader nei post o nelle pagine WordPress aggiunge gli attributi HTML width e height. La ragione è semplice: questi attributi consentono un caricamento più rapido delle immagini nel browser. Ma se per esempio stiamo progettando un tema responsive potremmo non averne bisogno. Vediamo come eliminarli.

Aggiungete il seguente codice al file functions.php:


add_filter( 'post_thumbnail_html', 'remove_dimensions_attributes', 10 );
add_filter( 'image_send_to_editor', 'remove_dimensions_attributes', 10 );

function remove_dimensions_attributes( $html ) {
   $html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
   return $html;
}