Remove Image attributes WordPress Editor

This small plugin will remove the width and height attributes from images when WordPress adds them to the post content.

Why is it so awesome?

In my case I'm always using the original file size for my Photography blog. I had a small jQuery snippet in charge of removing this attributes on page load. I want the images to be responsive and even though this can be accomplished by CSS it's just easier if there's no attributes.

Yes, browser paint will be slower.

I'm totally aware of that. Images without size attributes will paint/render slower on your browser (would you notice the difference?). In a perfect world you would define width and height for all images.

<?php

/*
Plugin Name: Remove Image Attributes
Plugin URI: http://php.quicoto.com
 * Description: This removes the width and height when WordPress adds the image to the Editor Content
 * Version: 1.0
 * Author: Ricard Torres
 * Author URI: http://php.quicoto.com
*/

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

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

About Rick

Senior Front-end Software Engineer from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.

Leave a Reply

Add <code> Some Code </code> by using this tags.

*
*