Created
March 1, 2019 21:44
-
-
Save txid0x7f/2c9d31c5898e8c926c1bb930ed10ed9c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
add_shortcode('wp_caption', 'fixed_img_caption_shortcode'); | |
add_shortcode('caption', 'fixed_img_caption_shortcode'); | |
function fixed_img_caption_shortcode($attr, $content = null) | |
{ | |
if (!isset($attr['caption'])) { | |
if (preg_match('#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)(.*)#is', $content, $matches)) { | |
$content = $matches[1]; | |
$attr['caption'] = trim($matches[2]); | |
} | |
} | |
$output = apply_filters('img_caption_shortcode', '', $attr, $content); | |
if ($output != '') | |
return $output; | |
extract(shortcode_atts(array( | |
'id' => '', | |
'align' => '', | |
'width' => '', | |
'caption' => '' | |
), $attr)); | |
if (1 > (int)$width || empty($caption)) | |
return $content; | |
if ($align == 'alignleft') $align = 'content__image-box--align--left'; | |
if ($align == 'aligncenter') $align = 'content__image-box--align--center'; | |
if ($align == 'alignright') $align = 'content__image-box--align--right'; | |
if ($align == 'alignnone') $align = ''; | |
return '<div class="content__image-box ' . $align . ' image-box" >' | |
. do_shortcode($content) . '<p class="image-box__caption">' . $caption . '</p></div>'; | |
} | |
function add_responsive_class($content) | |
{ | |
$content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8"); | |
$document = new DOMDocument(); | |
libxml_use_internal_errors(true); | |
$document->loadHTML(utf8_decode($content)); | |
$images = $document->getElementsByTagName('img'); | |
foreach ($images as $image) { | |
$existing_class = $image->getAttribute('class'); | |
$image->setAttribute('class', "image $existing_class"); | |
} | |
$html = $document->saveHTML(); | |
return $html; | |
} | |
add_filter('the_content', 'add_responsive_class'); | |
function disable_srcset($sources) | |
{ | |
return false; | |
} | |
add_filter('wp_calculate_image_srcset', 'disable_srcset'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment