Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Kristoffer Lund
Kristoffer Lund
11,133 Points

Wordpress Posts - Add class to Media.

What is the best way to automatically add the .img-responsive bootstrap class to the images you insert then adding a posts"?

I have searched around, but i am yet to find at solution that works.

2 Answers

Hi Kristoffer,

I haven't done this myself so can't give personal experience but this question on Stack Overflow seems to ask exactly the same question.

There has been an answer given but not marked as accepted. Hopefully you find it useful either way.

-Rich

Kristoffer Lund
Kristoffer Lund
11,133 Points

Perfekt, thank you. That worked.

For those who have the same problem, it works you if add

function add_image_responsive_class($content) { global $post; $pattern ="/<img(.*?)class=\"(.*?)\"(.*?)>/i"; $replacement = '<img$1class="$2 img-responsive"$3>'; $content = preg_replace($pattern, $replacement, $content); return $content; } add_filter('the_content', 'add_image_responsive_class');

to the functions.php

No problem. Glad you managed to get it sorted :)