WordPress uses Lazyload to implement IMG tag picture delay loading

Source: Internet
Author: User

In the use of lazyload in order to really achieve the effect of the booth map into the SRC attribute, the original image into the data-original attribute, a change is more troublesome, here we can use the buffer and regular batch to help us to replace:

The code is as follows Copy Code

function Bing_filter_lazy ($content) {
if (Is_feed ()) return $content;
return Preg_replace_callback ('/(<\s*img[^>]+) (src\s*=\s* "[^"]+ ") ([^>]+>)/I ', ' bing_filter_lazy_ Replace ', $content);
}

Retrofit IMG Tags
function Bing_filter_lazy_replace ($matches) {
if (!preg_match ('/class\s*=\s* "/I ', $matches [0])) $class _attr = ' class= '" ';
$replacement = $matches [1]. $class _attr. ' Src= '. Get_bloginfo (' Template_directory '). '/images/grey.gif '. ' Data-original '. SUBSTR ($matches [2], 3). $matches [3];
$replacement = preg_replace ('/class\s*=\s* '/I ', ' class= ' lazy ', $replacement);
$replacement. = ' <noscript> '. $matches [0]. ' </noscript> ';
return $replacement;
}

Control buffers
function bing_filter_lazy_html () {
Ob_start (' Bing_filter_lazy ');
}
if (!is_admin ()) add_action (' wp_loaded ', ' bing_filter_lazy_html ');
Note the address of the bitmap.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.