Remove inline CSS / Style attribute in featured image (Post thumbnail) In WordPress
0
up 2 down 0
starvotestarvotestarvotestarvotestarvote
Thank you for voting !

Remove inline CSS / Style attribute in featured image (Post thumbnail) In WordPress


While creating my company’s website, Once I was in big trouble due to Style attribute Inline CSS generating automatically on tag in Post thumbnail HTML (Featured image of Post). I tried almost every WordPress filter available online but none of them worked as expected. I never wanted to use jQuery or any Front-end script for removing the style attribute on HTML of Post thumbnail.

Before we proceed with the solution, I want my readers to understand Practically what I wanted to fix. Look at the below screenshot.

This blog post will indirectly answer all of the below Topics.

Topics Highlights –


How do I remove inline style in featured image markup?
Filter to remove image dimension attributes?
Stop WordPress from hardcoding img style attributes
Stop WordPress from adding style attribute to featured image
How to remove inline CSS from post thumbnails in WordPress
Filter Inline CSS in HTMl of featured image
Get rid of Inline CSS WordPress Post thumbnails HTML

style attribute post thumbnail issue

I wanted to remove this style attribute from WordPress Featured image Post thumbnail HTML. The solution I am going to tell is no where online and I used my mind to create this code. Actually this code is the combination of 2 codes available online. I combined wordpress filter post_thumbnail_html and Preg_replace (Preg_replace to filter only style attribute). This is how it worked and I was able to remove the Style Inline CSS after using the below code. Paste the code in functions.php and save it.

Best working Solution to fix Inline CSS style attribute for Post thumbnail HTML output


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

function remove_thumbnail_dimensions( $html ) {
		$html = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $html);
    return $html;
}

Something To Say ?

Your email address will not be published. Required fields are marked *

*