Skip to content

Instantly share code, notes, and snippets.

@javierarcheni
Last active December 7, 2017 11:27
Show Gist options
  • Save javierarcheni/c6ebf7634dae927ef544c3ef36c7acc2 to your computer and use it in GitHub Desktop.
Save javierarcheni/c6ebf7634dae927ef544c3ef36c7acc2 to your computer and use it in GitHub Desktop.
Añadir clase responsive a cada imagen del contenido WordPress
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