1

I would like to use Qazy Lazy load and I need to include new data attribute data-qazy="true" inside all img tags. I tried to include with this function, but it only applies for sidebar images and doesn't work for the post images:

function add_lazyload_atts( $atts, $attachment ) {
    if ( ! wp_get_attachment_image_src( $attachment ) ) {
    $atts['data-qazy'] = 'true';
    }
    return $atts;
}
add_filter( 'wp_get_attachment_image_attributes', 'add_lazyload_atts', 10, 2 );
3
  • 1
    Try researching the codex before just blatantly asking someone to tell you how to do something and reformat your question with examples of what you have tried, and what has failed. You could have also done a simple search on this site to find a possible solution. wordpress.stackexchange.com/questions/53735/… Sep 9, 2016 at 23:00
  • I tried this solution but it only applies the newly inserted images.
    – tw8sw8dw8
    Sep 10, 2016 at 5:52
  • Your problem is that this function is only in use in the sidebar. Look up what functions you use for the other content you want to lazy load, file and edit, etc.
    – kaiser
    Sep 11, 2016 at 14:35

1 Answer 1

5

I found the solution to add that attribute to the img tag with this function:

add_filter('the_content','new_content');
function new_content($content) {
    $content = str_replace('<img','<img data-qazy="true"', $content);
    return $content;
}
1
  • 1
    Glad that you solved your own question. Please be sure to click the checkmark next to your answer after two days to indicate that your question has been resolved. Sep 12, 2016 at 0:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.