Created
July 4, 2016 11:00
-
-
Save madila/c0744331765a1b415b85c8c2583ee43b to your computer and use it in GitHub Desktop.
Lazysizes filter for wordpress post thumbnail attachment
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
<?php | |
/** | |
* Created by PhpStorm. | |
* User: Madila | |
* Date: 21/06/2016 | |
* Time: 11:58 | |
*/ | |
add_filter('wp_get_attachment_image_attributes', 'lazysizes_data_src', 3); | |
function lazysizes_data_src($attr) { | |
if(array_key_exists('data-lazysizes', $attr) && $attr['data-lazysizes']) { | |
$attr['class'] = $attr['class'].' lazyload'; | |
$attr['data-src'] = $attr['src']; | |
$attr['src'] = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'; | |
} | |
return $attr; | |
} | |
add_filter('post_thumbnail_html', 'intrinsic_ratio', 10, 4); | |
function intrinsic_ratio($html, $post_id, $post_image_id, $size) | |
{ | |
$full_size = wp_get_attachment_image_src($post_image_id, $size); | |
if (strpos($html, 'data-intrinsic') > -1) { | |
$ratio = ($full_size[2] / $full_size[1]) * 100; | |
$html = '<picture class="img-intrinsic entry-thumbnail" style="padding-top:' . $ratio . '%">' . $html . '</picture>'; | |
} | |
return $html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment