Created
February 4, 2017 00:17
-
-
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"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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