I’m wondering if there’s a simple way top stop WordPress automatically hardcoding featured image height and width attributes, other than using regex…
As I’m using a flexible grid for my project (who isn’t!) this is causing some funky image issues.
I’m wondering if there’s a simple way top stop WordPress automatically hardcoding featured image height and width attributes, other than using regex…
As I’m using a flexible grid for my project (who isn’t!) this is causing some funky image issues.
You must be logged in to post a comment.
You can get featured image URL and add it to your content manually, eg:
You can remove the width and height attributes by filtering the output of
image_downsize
function found atwp-includes/media.php
. To do this, you write your own function and execute it via your theme’s functions.php file or as a plugin.Example:
Remove the
width
andheight
attributes.Attach the new function to the
image_downsize
hook:Also don’t forget to scale the images correctly in your CSS:
Hope this helps you.
Cheers,
You can use the
post_thumbnail_html
filter to remove the attribute:Place this in your
functions.php
fileYou can overrule inline styles/attributes with
!important
:It’s not the cleanest solution, but it solves your problem.
Best solution is to place jquery in footer
CSS solution:
This allows responsive images to work as they should, meanwhile you maintain the width and height attributes in the img element, which is probably better for older browsers, performance and/or to pass HTML validators.
PHP solution:
This will prevent the addition of width/height attributes on any newly-added media in the WP editor (via ‘Add Media’). FYI, it may affect your image captions too!
Here is a simple Javascript solution:
This can be used to target specific images instead of all images by using a CSS selector, like so:
Add this piece of code: (Applies to new Gutenberg editor too)