Making WordPress images responsive
Method 1:
The CSS
Add the following code to your CSS file. That will make the images scalable according to screen size.
Removing automatic height and width in WordPress
tags
Now drag the window to see the image scaling in action. You’ll notice that the images in your WordPress blog scale weirdly. They horizontal scaling fine but vertical scaling in WordPress images are all wrong.
To make the images resizable proportionately in WordPress, We have to remove the automatic width and height values WordPress add for < img > tags.
As an example, We have to change this:
To This:
For the images that are in a post or a static page/template page, all you have to do is, add the above CSS to the style.css file, and then remove the ‘width’ and ‘height’ properties from the < img > tag in your WordPress editor. That’s it!
But for the image that are displayed dynamically by WordPress, such as post thumbnails, the width and height needs to be removed dynamically using a function.
Add the following function to your functions.php file.
return preg_replace(‘/\/i’, ”,$string);
}
Then when you call for those post thumbnail images in you template.php page, replace,
With This:
That’s it. Drag and resize the browser to see your responsive WordPress images in action! Here’s a demo.
Method 2:
The above will not work for some themes.
If you are one of the few that it did not work, you can still get your image issue solved using below function.
Add the following function to your functions.php file.
This removes inline width and height attributes from images retrieved with
,
and prevents those attributes from being added to new images added to the editor.
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( '/(width|height)=\"\d*\"\s/', "", $html ); return $html; }
This did the trick for me:
function remove_wp_width_height($string){
return preg_replace(‘/\i’, ”,$string);
}
You actually made several terrific ideas throughout your article, “Making WordPress
images responsive | Blissful Interfaces”.
I’ll remain coming to ur blog soon. Thx ,Zella
It worked! You are the man!
Check it out at http://ComedianMan.com
Thank you thank you thank you!
~Keep Having FUN!
The code above won’t work for me.
I find other solution.
http://wordpress.stackexchange.com/questions/5568/filter-to-remove-image-dimension-attributes
You could also do it without altering the code used in the template by adding a hook
function remove_wp_width_height($html){
return preg_replace(‘/\/i’, ”,$html);
}
add_filter( ‘post_thumbnail_html’, “remove_wp_width_height”, 10,1);
Now you can keep using “the_post_thumbnail();”
Thank you so much for this tutorial
I can find my images responding according to the screen size!
Hi Thanushka.
Thank you for this tutorial.
However it didn’t work for me. : (
Every step of the tutorial worked as it should untill I called the function -> the image did not display. I then tested the function call by having it simply return the $string and it showed the image.
So I used a jquery function instead.
But I am determined why your solution wouldn’t work (I prefer PHP to jQuery in this case).
Can you explain what the ‘/\/i’ in preg_replace(‘/\/i’, ”,$string) is?
Thank you in advance…
-Sasha
Thanks for this tutorial, I’ve already started using some of the tips.
However I’ve found that I can’t get images in galleries created with the WordPress gallery shortcode to display correctly. They retain the same height as their width decreases, meaning they appear distorted.
This is most likely because the width and height are specified by default in the code for each image, e.g. as follows:
Presumably we need some kind of code hook to prevent it automatically specifying the image width and height, but I don’t know how to write one. Can anyone help?
I have one image the div id=”banner” image which is the leading banner image on the homepage. Is it possible to make this responsive so that the image fits on a screen with resolution 1600 x 900 fine then is scaled down to 1024 x 768 automatically, as now it is fine on the later but looks strange on a bigger screen.
Thanks in advance
You can use 2 images and load each with CSS media queries for smaller screens and bigger screens.
Try this tutorial for CSS media queries
A different approach would be to make the image ‘seamless’ with editing it’s offset in photoshop and repeat it as a background image.
This tutorial is really well written.
It’s language is simple and easy to understand. I was thrilled to read through it.
I’ve shared this and the WP template creation tutorial on my Facebook pages and Tweeted it across my Twitter accounts.
Thanks Thanushka really appreciate this body of work.
If you’ve a few minutes spare do take a quick look at http://www.opensourcevarsity.com. There’s a ton of tutorials on Opensource tools and technologies on the site.
I wonder if you would consider writing Guest Posts for OSV.
Do get in touch.
Regards,
Ivan Bayross
Thank you Ivan. I checked your site and it’s filled with great tutorials. I think it’s really great of you to spare your valuable time to make the internet a better place.
I’d be very happy to make you some time in my schedule too.
Hi Thanushka,
Thanks for replying to my post. If you would send me any CSS Blog post that you’d like published on the opensourcevarity Blog, I’d be happy to publish it. Feel free to add up to two links in the Blog post or Signature or use both.
It would be a privilege to publish your stuff which is so informative and helpful.
Do write in as soon as possible.
Regards,
Ivan Bayross
5.2.11
Thanks Thanushka!
Replacing the call below produced “Warning: Wrong parameter count for preg_replace() in ” errors:
$thumbnails = get_posts(‘numberposts=5&category=613&orderby=date&order=DESC’);
foreach ($thumbnails as $thumbnail) {
if ( has_post_thumbnail($thumbnail->ID)) {
echo ‘ID ) . ‘” title=”‘ . esc_attr( $thumbnail->post_title ) . ‘”>’;
// echo get_the_post_thumbnail($thumbnail->ID, ‘thumbnail’); // replaced with call below
echo remove_wp_width_height(get_the_post_thumbnail(get_the_ID(),’large’));
echo ‘‘;
}
}
Does your host run PHP 5? That’s usually the reason behind “Wrong parameter count for preg_replace()”. Also this warning means you are passing empty parameters
this WordPress responsive images post is blissful… thanks!
Of course, what a great website and information posts, I definitely will bookmark your blog.All the Best! Add article to my site.
Thanks for sharing this! I have been looking everywhere for a solution for responsive thumbs in WP.
Its not working for me tho: the code for functions.php seems to be breaking my theme. Also, how would I incorporate the code in the template page if my code looks like the following?:
Thanks in advance,
- j
Hi J,
Your code was not publushed. You can repost or email me at [email protected]. I’ll check it out.
The code for functions php file should be added between, < ? Php> tags.
Sry abt that— the template code is:
In regards to the code for functions.php: I am wrapping it in tag, and still it breaks. I am assuming this is b/c of how my file is setup?
I see php is not show up in these comments again. So, the code is the following wrapped in php tag:
if(has_post_thumbnail()){
the_post_thumbnail(‘thumbnail’);
}
if(has_post_thumbnail()){
echo remove_wp_width_height(get_the_post_thumbnail(get_the_ID(),’large’));
}
hmm…still not working for me. I am getting a parsing error on the functions.php code, and the template page code doesn’t seem to work. Is there anything I need to do on the admin panel, ex: the default thumb sizes, etc?
thanks for your patience!
How is the widths and heights in CSS and HTML of your theme? Before the images could be scalable, the html containers (divs etc) they are in, should scale. The width and heights of containers should be % instead of fixed values, and should be controlled through min and max values when they should look fixed, if you want them to be scalable.
Ex:
Div1{
width: 30%;
Min-value: 100px;
Max-value: 400px;
}
for the functions error, try typing the code rather than pasting, that works for me when there isn’t a real reason why it breaks. And look for other references in the code for the same thumbnails or images. You might have to add it to one of those places. ( I couldn’t find your mail to look at it)
Thanks for the response Thanushka— I have resolved the issue. The containers around the img’s where properly set in %, and the issue w/ the functions.php file must have been b/c I was pasting it, and not typing.
The code I used is:
if(has_post_thumbnail()) {
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),’thumbnail’ );
echo ”; }
This seems to be working well— does this look like a future-proof / sensible alt. solution? Thanks for all the help!
Nice blog, thanks for sharing
I took a fresh look and sorted it out. Many thanks again for the great post and the help it provided.
Many thanks for this. It is working for me—almost. It is scaling image horizontally but not vertically. If you have a moment and have any idea why, I would appreciate you letting me know.
Hi Steve,
Try adding height: auto; to img tag in your CSS, along with width: 100%;
I usually remove width and height from tags in the posts and use a function ( as in that post) when I need to remove the width and height dynamically. Hope this helps..