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. |