Skip to content

Instantly share code, notes, and snippets.

@CoachBirgit
Last active April 16, 2018 17:21
Show Gist options
  • Save CoachBirgit/e2c79015dae3026d7b56 to your computer and use it in GitHub Desktop.
Save CoachBirgit/e2c79015dae3026d7b56 to your computer and use it in GitHub Desktop.
WordPress: Add 'img-responsive' class via filter to 'the_content'
// add default class for img-responsive
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');
@matgargano
Copy link

don't need the global $post

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment