Last active
December 7, 2017 11:27
-
-
Save javierarcheni/c6ebf7634dae927ef544c3ef36c7acc2 to your computer and use it in GitHub Desktop.
Añadir clase responsive a cada imagen del contenido WordPress
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
function add_responsive_class_content($content){ | |
$content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8"); | |
if (!empty($content)) { | |
$document = new DOMDocument(); | |
libxml_use_internal_errors(true); | |
$document->loadHTML(utf8_decode($content),LIBXML_HTML_NOIMPLIED); | |
$imgs = $document->getElementsByTagName('img'); | |
foreach ($imgs as $img) { | |
$existing_class = $img->getAttribute('class'); | |
$img->setAttribute('class', "img-responsive $existing_class"); | |
} | |
$html = $document->saveHTML(); | |
$html = str_replace(”,”,$html); | |
return $html; | |
} | |
else { | |
return $content; | |
} | |
} | |
add_filter('the_content', 'add_responsive_class_content'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment