Lazy Images: better support for content loaded via AJAX
Whenever I use AJAX to load content from another page of my site, only the 1×1 placeholder comes in. How do I reinitialize JetPack’s lazy images to pop the proper image back in after Ajax?
Just a basic jQuery load, loading HTML from one page into the current one. -- https://wordpress.org/support/topic/using-ajax-only-loads-1x1-lazy-placeholder/
For now, here is a work around:
function update_thumbs() {
$('YOUR IMAGE SELECTOR').each(function() {
var src = $(this).prop('src');
if (src.indexOf('1x1.trans.gif') !== -1) {
$(this).prop('src', $(this).data('orig-file'));
}
});
}
This issue has been marked as stale. This happened because:
- It has been inactive in the past 6 months.
- It hasn’t been labeled `[Pri] Blocker`, `[Pri] High`.
No further action is needed. But it's worth checking if this ticket has clear reproduction steps and it is still reproducible. Feel free to close this issue if you think it's not valid anymore — if you do, please add a brief explanation.
An alternative would be to use the following after loading the content via AJAX:
$(document.body).trigger('post-load');
This triggers a jQuery event that the lazy load module is listening for in order to lazy load images.
You could also add the 'skip-lazy' CSS class to the AJAX loaded content.
This issue has been marked as stale. This happened because:
- It has been inactive in the past 6 months.
- It hasn’t been labeled `[Pri] Blocker`, `[Pri] High`.
No further action is needed. But it's worth checking if this ticket has clear reproduction steps and it is still reproducible. Feel free to close this issue if you think it's not valid anymore — if you do, please add a brief explanation.
Another possible report in https://wordpress.org/support/topic/lazy-load-images-no-longer-work-with-ajax/
I've suggested the workaround above to see if that helps.
Best option will be disabling lazy load for ajax calls, add following code in function.php
function shankarg_dev_lazyload_exclude() {
if( ! empty( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) && strtolower( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ]) == 'xmlhttprequest' ){
return false;
}
return true;
}
add_filter( 'lazyload_is_enabled', 'shankarg_dev_lazyload_exclude', 15 );
Jetpack Lazy Images doc https://jetpack.com/support/lazy-images/
You can also check for ajax request using
if(wp_doing_ajax()) {
return false;
}
but in my case wp_doing_ajax() is not working. so I check with request header.