Skip to content

Instantly share code, notes, and snippets.

@bkaminski
Created February 4, 2017 00:17
Show Gist options
  • Save bkaminski/68cb138b282ddaad339ca2a4a2464752 to your computer and use it in GitHub Desktop.
Save bkaminski/68cb138b282ddaad339ca2a4a2464752 to your computer and use it in GitHub Desktop.
Automatically add the Bootstrap 4 "img-fluid" class to any uploaded image through the WordPress dashboard. Add to "functions.php"
<?php
//Wordpress Fluid Images Bootstrap 4.0.0-alpha.4
function bootstrap_fluid_images( $html ){
$classes = 'img-fluid'; // Bootstrap 4.0.0-alpha.4
// check if there are already classes assigned to the anchor
if ( preg_match('/<img.*? class="/', $html) ) {
$html = preg_replace('/(<img.*? class=".*?)(".*?\/>)/', '$1 ' . $classes . ' $2', $html);
} else {
$html = preg_replace('/(<img.*?)(\/>)/', '$1 class="' . $classes . '" $2', $html);
}
// remove dimensions from images,, does not need it!
$html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );
return $html;
}
add_filter( 'the_content','bootstrap_fluid_images',10 );
add_filter( 'post_thumbnail_html', 'bootstrap_fluid_images', 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment