Skip to content

Instantly share code, notes, and snippets.

@13twelve
Created December 8, 2016 15:05
Show Gist options
  • Save 13twelve/9f8234f29ba459cf27c85b68be276a55 to your computer and use it in GitHub Desktop.
Save 13twelve/9f8234f29ba459cf27c85b68be276a55 to your computer and use it in GitHub Desktop.
Wyss responsive post images
function wyss_responsive_post_images( $content ){
// unwrap images of links, except the one in the video embed
$content = preg_replace('/(?<!\<\/iframe>)<a.*?>(<img.*?>)<\/a>/', '$1', $content);
// wrap imgs in figures (if not in figures)
$content = preg_replace('/(\s|^)(<img.*?( class=[\'\"].*?[\"\']).*?>)(\s|$)/', '<figure$3>$2</figure>', $content);
// remove style from figure
$content = preg_replace('/(figure[^>]*) style=(\"|\')+[a-zA-Z0-9:;\.\s\(\)\-\,\#\_]*(\"|\')/', '$1', $content);
// remove ID from figures
$content = preg_replace('/(figure[^>]*) id=(\"|\')+[a-zA-Z0-9:;\.\s\(\)\-\,\#\_]*(\"|\')/', '$1', $content);
// unwrap <div class="entry-content-asset">
$content = preg_replace('/<div class=(?:\"|\')entry-content-asset(?:\"|\')>\n<figure>([\S\s]+?)<\/figure>\n<\/div>/', '<figure>$1</figure>', $content);
if ( ! preg_match_all( '/(<figure [^>]+>)(<img [^>]+>)/', $content, $matches ) ) {
return $content;
}
$selected_images = $attachment_ids = array();
$figures = array();
foreach( $matches[1] as $figure ) {
array_push($figures,$figure);
}
foreach( $matches[2] as $image ) {
if ( false === strpos( $image, ' srcset=' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) &&
( $attachment_id = absint( $class_id[1] ) ) ) {
/*
* If exactly the same image tag is used more than once, overwrite it.
* All identical tags will be replaced later with 'str_replace()'.
*/
$selected_images[ $image ] = $attachment_id;
// Overwrite the ID when the same image is included more than once.
$attachment_ids[ $attachment_id ] = true;
}
}
if ( count( $attachment_ids ) > 1 ) {
/*
* Warm object cache for use with 'get_post_meta()'.
*
* To avoid making a database call for each image, a single query
* warms the object cache with the meta information for all images.
*/
update_meta_cache( 'post', array_keys( $attachment_ids ) );
}
$index = 0;
foreach ( $selected_images as $image => $attachment_id ) {
$image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
$img_options = array();
$figure_align = "alignnone";
if(preg_match('/(align.*?)(?:\s|")/', $figures[$index], $align)) {
$figure_align = trim($align[1]);
}
switch ($figure_align){
case 'alignleft':
$img_options = array(
'w' => $image_meta['width'],
'h' => $image_meta['height'],
'sizes' => array(
array(
'media' => 'xlarge+',
'w' => '467px'
),
array(
'media' => 'xlarge',
'w' => 'calc( (100vw - 160px) / 3 )'
),
array(
'media' => 'large',
'w' => 'calc( (100vw - 120px) / 3 )'
),
array(
'media' => 'medium',
'w' => 'calc( (100vw - 120px) / 3 )'
),
array(
'media' => 'xsmall',
'w' => 'calc(100vw - 40px)'
),
),
'srcset' => array(2000,1600,1200,800,400)
);
break;
case 'alignright':
$img_options = array(
'w' => $image_meta['width'],
'h' => $image_meta['height'],
'sizes' => array(
array(
'media' => 'xlarge+',
'w' => '690px'
),
array(
'media' => 'xlarge',
'w' => 'calc( (100vw - 120px) / 2 )'
),
array(
'media' => 'large',
'w' => 'calc( (100vw - 90px) / 2 )'
),
array(
'media' => 'medium',
'w' => 'calc( (100vw - 90px) / 2 )'
),
array(
'media' => 'xsmall',
'w' => 'calc(100vw - 40px)'
),
),
'srcset' => array(2000,1600,1200,800,400)
);
break;
default:
$img_options = array(
'w' => $image_meta['width'],
'h' => $image_meta['height'],
'sizes' => array(
array(
'media' => 'xlarge+',
'w' => '1177px'
),
array(
'media' => 'xlarge',
'w' => 'calc( ((( (100vw - 80px) - (11 * 40px)) / 12) * 10) + 360px )'
),
array(
'media' => 'large',
'w' => 'calc( ((( (100vw - 60px) - (11 * 30px)) / 12) * 11) + 300px )'
),
array(
'media' => 'medium',
'w' => 'calc(100vw - 60px)'
),
array(
'media' => 'xsmall',
'w' => 'calc(100vw - 40px)'
),
),
'srcset' => array(2000,1600,1200,800,400)
);
break;
}
$generated_img = wyss_get_image_tag($attachment_id, $img_options);
$content = str_replace( $image, $generated_img, $content );
$index++;
}
return $content;
}
add_filter( 'the_content', 'wyss_responsive_post_images', 99 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment